database_cleaner 在大多数情况下都有效,但在某些情况下,我希望在每次测试后清除 user_id 之类的东西,但事实并非如此。因此,user_id 将自始至终递增而不是清除,并且用户 id 可预测为 1、2、3 或为测试创建的许多。我可以只调用 id 而不是对预期结果进行硬编码,但稍后我真的需要它来清除更复杂示例中的内容。这是最容易展示的。任何帮助将不胜感激。
从 SPEC_HELPER.RB:
RSpec.configure do |config|
config.mock_with :rspec
config.include FactoryGirl::Syntax::Methods
config.include(Capybara, :type => :integration)
config.include Devise::TestHelpers, :type => :controller
config.use_transactional_fixtures = false
config.before(:each) do
I18n.default_locale = :en
I18n.locale = :en
DatabaseCleaner.start
ResqueSpec.reset!
ActionMailer::Base.deliveries.clear
end
config.after(:each) do
DatabaseCleaner.clean
end
config.after(:all) do
TestCleaner.clean
end
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
DatabaseCleaner.strategy = :transaction
Role.reset_cache!
end
config.after(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
从我的测试:
it "should return one provider" do
get :index
response.body.gsub(/\s+/, "").should == {
:experts => [{
:availability => false,
:name => "#{@provider.user.first_name}#{@provider.user.last_name}",
:expert_id => 1,
:photo => @provider.details.photo.url
}] }.to_json
end
it "should show return two providers" do
@provider2 = create(:provider)
get :index
response.body.gsub(/\s+/, "").should == {
:experts => [{
:availability => false,
:name => "#{@provider.user.first_name}#{@provider.user.last_name}",
:expert_id => 1,
:photo => @provider.details.photo.url
},
{
:availability => false,
:name => "#{@provider.user.first_name}#{@provider.user.last_name}",
:expert_id => 2,
:photo => @provider.details.photo.url
}
] }.to_json
end