3

我有一个测试用例:

describe User do
  let(:user) { create(:user) } # FactoryGirl
  it { User.count == 1 }  
end

并设置:

RSpec.configure do |config|
  config.include FactoryGirl::Syntax::Methods
  config.before(:suite) { DatabaseCleaner.strategy = :truncation }
  config.before(:each) { DatabaseCleaner.start }
  config.after(:each) { DatabaseCleaner.clean }
  config.use_transactional_fixtures = false
end

为什么它不工作?如何使用 let 并得到一个真正持久的记录?

4

1 回答 1

5

let调用是惰性求值的,除非调用特定方法,否则它们不会运行:

describe User do
  subject { user }
  let(:user) { create(:user) } # FactoryGirl
  it { … }  
end

或者,let!用于始终评估的前提条件:

describe User do
  let!(:user) { create(:user) } # FactoryGirl
  it { … }  
end
于 2012-04-12T07:59:47.710 回答