【发布时间】:2015-11-20 20:44:02
【问题描述】:
我正在我的控制器规范中运行一些测试,但我发现了一个奇怪的行为,我无法解释为什么会发生这种情况。
我的规格如下所示:
require 'rails_helper'
describe BooksController do
let(:user_with_books) { create :user, :with_books }
...
describe 'GET /books/:book_id/owners' do
it 'shows all owners of the book' do
book = user_with_books.books.first
user_2 = create :user
book.owners << user_2
get :owners, book_id: user_with_books.books.first.id
expect(assigns(:users).count).to eq 2
expect(assigns(:users).first).to eq user_with_books
expect(assigns(:users).second).to eq user_2
end
end
end
如果我运行命令
rspec spec/controllers/books_controller_spec.rb:31 一切都是绿色的:
但如果我只运行rspec,测试将失败!
Rspec 在这个规范上做了什么来改变行为?我可以做些什么来解决这个问题?
编辑:我的 spec_helper.rb
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
config.infer_spec_type_from_file_location!
config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
end
end
【问题讨论】:
-
听起来你有
DatabaseCleaner在测试执行之间清理数据库。请分享您的RSpec.config(应在spec_helper.rb -
我现在将它添加到我的问题中
-
你如何执行
DatabaseCleaner.clean?在before(:each)? -
没有设置。这个 rails_helper 的 sn-p 都包含在 DatabaseCleaner 中。
-
这就是问题所在,谢谢它的工作:)
标签: ruby-on-rails ruby rspec