我已经阅读了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
最佳答案
因为before块正在调用visit user_path(user)
,所以用户值在那里被初始化,并且RSpec将访问该页面。如果:m1
:m2
未使用let!
,则访问将不会产生任何内容
it { should have_content(m1.content) }
it { should have_content(m2.content) }
失败,因为它希望在用户访问页面之前创建微信息。
let!
允许在调用before块之前创建微博,并且当测试访问页面时,应该已经创建了微博。编写相同测试并使它们通过的另一种方法是执行以下操作:
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 do
m1
m2
visit user_path(user)
end
在
m1
之前调用变量m2
和visit user_path(user)
会导致它们在访问页面之前被初始化,并导致测试通过。更新
这个小例子更有意义:
在此示例中,我们调用get_all_posts,它仅返回帖子数组。注意,我们在断言之前和
it
块执行之前调用该方法。因为post在执行断言之前不会被调用。def get_all_posts
Post.all
end
let(:post) { create(:post) }
before { @response = get_all_posts }
it 'gets all posts' do
@response.should include(post)
end
通过使用
let!
,该帖子将在RSpec看到该方法后立即创建(在before
块之前),并且该帖子将在Post
列表中返回同样,另一种方法是在调用方法之前,在before块中调用变量名。
before do
post
@response = get_all_posts
end
这样可以确保在调用方法本身创建
let(:post)
之前先调用Post
块,以便在Post.all
调用中将其返回
关于rspec - 区分Rspec的 'let'与 'let!'的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17407733/