我已经阅读了rspec 文档并搜索了许多其他地方,但我很难掌握 Rspeclet
和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