【发布时间】:2012-10-30 05:55:47
【问题描述】:
我正在创建一个 rspec 测试来查看 create 方法中的实例变量是否保留了构建的数据。但是,我的测试不起作用,因为我返回此错误...
Failure/Error: assigns[:micropost]should eq(@post)
expected: #<Micropost id: 1, content: "Hello there", user_id: 1>
got: #<Micropost id: 2, content: "Hello there", user_id: 1>
我的 rspec 测试是
describe ::MicropostsController do
before :each do
@post = FactoryGirl.create(:micropost)
end
it "tests the instance variable in create method" do
post :create, micropost: FactoryGirl.attributes_for(:micropost)
assigns(:micropost).should eq(@post)
end
我的 FactoryGirl 文件是
FactoryGirl.define do
factory :micropost do
content "Hello there Bob!"
user_id "1"
#even if I got rid of the double quotations around 1, the stringify key error still
#pops up
end
end
这里是微柱控制器创建动作代码...
def create
@micropost = Micropost.new(params[:micropost])
respond_to do |format|
if @micropost.save
format.html { redirect_to @micropost, notice: 'Micropost was successfully create.'
}
else
format.html { render action: "new" }
end
end
end
【问题讨论】:
-
我已经用你的控制器代码编辑了我的答案
标签: ruby-on-rails ruby-on-rails-3 testing rspec