我正在创建一个 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