我正在尝试测试用户创建成功的注释显示在用户的个人页面上。但是,无论何时运行我的测试,我都会收到折旧错误以及由于未定义用户而无法正常运行的测试。
user_page_spec.rb
let (:user) { FactoryGirl.create(:user) }
describe "user note list" do
before(:all) { 10.times { FactoryGirl.create(:note, user: user) } }
after(:all) { Note.delete_all }
factories.rb
FactoryGirl.define do
factory :user do
name "JowJebus"
provider "twitter"
uid "123456"
//matches omniauth fake user
factory :note do
sequence(:title) { |n| "Note_#{n}" }
sequence(:content) { |n| "Lorem ipsum ..... #{n}" }
user
结果错误:
undefined method 'notes' for nil:NilClass
和折旧错误
This is deprecated behavior that will not be supported in RSpec 3.
'let' and 'subject' declarations are not intended to be called
in a 'before(:all)' hook, as they exist to define state that...
所以显然我打算让这个测试的用户不正确,如何才能正确地做到这一点?
感谢。
注意:我正在测试的东西确实有效。我只需要帮助正确地进行测试。
答案 0 :(得分:0)
警告是因为let关键字旨在用于事务上下文,而before(:all)是一个事务外范围。
完整的信息非常明确:
let and subject declarations are not intended to be called
in a `before(:all)` hook, as they exist to define state that
is reset between each example, while `before(:all)` exists to
define state that is shared across examples in an example group.
您可以使用before(:all)
或
before(:each)
before(:all) do
user = FactoryGirl.create(:user)
10.times do
FactoryGirl.create(:note, user: user)
end
end
这也应该有用。