我正在尝试测试一些与rspec的关联。 这是我的测试:
1)
it 'creates an incubation with proper associations' do
expect{post :apply_startup, id: incubator.id, startup: {id: startup.id}}
.to change{incubator.pending_startups.count}.from(0).to(1)
end
2)
it 'creates an incubation with proper associations' do
expect{post :apply_startup, id: incubator.id, startup: {id: startup.id}}
.to change{incubator.pending_startups}.from([]).to([startup])
end
这是apply_startup
方法的定义:
def apply_startup
startup = Startup.find_by_id params[:startup][:id]
@incubator.pending_startups << startup if startup
redirect_to incubator_path(@incubator)
end
首次测试通过,但第二次测试失败。
编辑#1(失败规范的输出)
2) IncubatorsController#apply creates an incubation with proper associations
Failure/Error: expect{post :apply, id: incubator.id, startup: {id: startup.id}}
result should have initially been [], but was [#<Startup id: 665, ********]
有人可以解释一下,为什么? 感谢名单
答案 0 :(得分:0)
看起来您的数据库没有在每个规范之间重置。您使用的是database_cleaner
吗?