Ruby 嵌套RSpec测试
我正在学习迈克尔·哈特尔的教程,遇到了以下我难以理解的代码Ruby 嵌套RSpec测试,ruby,testing,rspec,nested,Ruby,Testing,Rspec,Nested,我正在学习迈克尔·哈特尔的教程,遇到了以下我难以理解的代码 describe "index" do let(:user) { FactoryGirl.create(:user) } before(:each) do sign_in user visit users_path end it { should have_title('All users') } it { should have_content('All users
describe "index" do
let(:user) { FactoryGirl.create(:user) }
before(:each) do
sign_in user
visit users_path
end
it { should have_title('All users') }
it { should have_content('All users') }
describe "pagination" do
before(:all) { 30.times { FactoryGirl.create(:user) } }
after(:all) { User.delete_all }
it { should have_selector('div.pagination') }
it "should list each user" do
User.paginate(page: 1).each do |user|
expect(page).to have_selector('li', text: user.name)
end
end
end
end
我的问题是:
这是一个嵌套测试,其中分页测试块在索引测试块内运行吗?换句话说,测试流程的顺序:
谢谢以下是上述测试的流程: 在以下各项之前执行
before(:each)
块:
it { should have_title('All users') }
it { should have_content('All users') }
然后,再次执行(:each)之前的,然后执行描述块
,该块执行:
before(:all) { 30.times { FactoryGirl.create(:user) } }
it { should have_selector('div.pagination') }
it "should list each user" do
User.paginate(page: 1).each do |user|
expect(page).to have_selector('li', text: user.name)
end
end
最后,在(:all){User.delete_all}
之后执行
我希望这有助于解释这个流程。如果分页块不是嵌套的,而是与索引块分离的话,这似乎更有意义。换句话说,为分页测试编写一个独立的登录用户并访问用户块。为什么?只有在访问索引
并且有足够的用户进行分页时,才会进行分页我想是的。谢谢你的帮助!