【发布时间】:2013-06-28 18:24:06
【问题描述】:
我已经阅读了rspec docs 并搜索了许多其他地方,但我很难理解 Rspec 的 let 和 let! 之间的区别
我读到let 直到需要它才被初始化,并且它的值仅在每个示例中被缓存。我还读到let! 强制变量立即存在,并强制对每个示例进行调用。我想因为我是新手,所以我很难看到这与以下示例有何关系。为什么:m1需要设置let!才能断言页面上存在m1.content,而:user可以设置let断言页面包含text: user.name?
subject { page }
describe "profile page" do
let(:user) { FactoryGirl.create(:user) }
let!(:m1) { FactoryGirl.create(:micropost, user: user, content: "Foo") }
let!(:m2) { FactoryGirl.create(:micropost, user: user, content: "Bar") }
before { visit user_path(user) }
it { should have_selector('h1', text: user.name) }
it { should have_selector('title', text: user.name) }
describe "microposts" do
it { should have_content(m1.content) }
it { should have_content(m2.content) }
it { should have_content(user.microposts.count) }
end
end
describe "after saving the user" do
before { click_button submit }
let(:user) { User.find_by_email('user@example.com') }
it { should have_selector('title', text: user.name) }
it { should have_success_message('Welcome') }
it { should have_link('Sign out') }
end
【问题讨论】: