tim*_*one 1 rspec ruby-on-rails ruby-on-rails-3
我在规范中有以下代码:
it 'should save some favorite locations' do
user=FactoryGirl.create(:user) # not adding anything
Run Code Online (Sandbox Code Playgroud)
它似乎没有写任何东西到数据库.FactoryGirl是否打算在模型规范中写一些东西?如果我从rails控制台运行,它会添加到数据库中.为什么rspec中的测试没有运行呢?它是如何工作的?
谢谢
如果已将rspec配置为对每个测试使用数据库事务,或者使用数据库截断,则会回滚或销毁所创建的任何记录.
要检查它是否真的在添加内容,您可以尝试:
it 'should save some favorite locations' do
user=FactoryGirl.create(:user) # not adding anything
User.find(user.id).should_not be_nil # ensure it is in database
Run Code Online (Sandbox Code Playgroud)
如果它通过,则将其添加到数据库中.
如果您正在为测试使用数据库事务,则在每次测试后回滚数据库.如果需要使用在多个测试中创建的记录,可以使用:
before(:all) do
@user=FactoryGirl.create(:user)
end
after(:all) do
@user.destroy # with create in before(:all), it is not in a transaction
# meaning it is not rolled back - destroy explicitly to clean up.
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2379 次 |
| 最近记录: |