Ruby on rails 这是RoR教程测试中的冲突吗?
我正在学习迈克尔·哈特尔的RubyonRails教程 在清单9.42中,他在索引页上显示了一个删除用户链接的测试 测试应该确保具有Ruby on rails 这是RoR教程测试中的冲突吗?,ruby-on-rails,ruby-on-rails-4,rspec,capybara,railstutorial.org,Ruby On Rails,Ruby On Rails 4,Rspec,Capybara,Railstutorial.org,我正在学习迈克尔·哈特尔的RubyonRails教程 在清单9.42中,他在索引页上显示了一个删除用户链接的测试 测试应该确保具有admin:true属性的用户在用户的索引页(用户的列表页)上看到delete链接 此外,管理员(第一个用户)不应该看到删除他自己的链接 测试代码如下所示: 这段代码让我困惑的是: 在我看来,descripe块中的第一个it子句似乎是合乎逻辑的 其中提到User.first的路径(这里是admin,因为admin是数据库中的第一个) 与descripe块中的第三个it
admin:true
属性的用户在用户的索引页(用户的列表页)上看到delete
链接
此外,管理员(第一个用户)不应该看到删除他自己的链接
测试代码如下所示:
这段代码让我困惑的是:
在我看来,descripe块中的第一个it
子句似乎是合乎逻辑的
其中提到User.first的路径(这里是admin,因为admin是数据库中的第一个)
与descripe块中的第三个it
子句冲突,该子句需要
指向管理员删除的链接不存在
我是不是遗漏了什么
我甚至还没有运行它,但在我看来它必须失败。当然,你应该运行它来找出实际情况。根据我的理解,这取决于
数据库
清理器的工作方式,如果它在每次运行测试时清理数据库(这是常见情况),则上述规范将通过 不是您的问题中显示了它,而是第一个it
块没有冲突,因为第一个用户是在该代码上方创建的
let(:user){FactoryGirl.create(:user)}
第三个it块是指当管理员签名时,它本身不应该有删除按钮
以下是#index
的完整规范:
subject { page }
describe "index" do
let(:user) { FactoryGirl.create(:user) } #First User Created Here
before do
sign_in user
visit users_path
end
it { should have_title('All users') }
it { should have_content('All users') }
describe "pagination" do
.
.
.
end
describe "delete links" do
it { should_not have_link('delete') }
describe "as an admin user" do
let(:admin) { FactoryGirl.create(:admin) } #Admin Created here
before do
sign_in admin
visit users_path
end
it { should have_link('delete', href: user_path(User.first)) }
it "should be able to delete another user" do
expect do
click_link('delete', match: :first)
end.to change(User, :count).by(-1)
end
it { should_not have_link('delete', href: user_path(admin)) }
end
end
end
it { should_not have_link('delete') }
describe "as an admin user" do
let(:admin) { FactoryGirl.create(:admin) }
before do
sign_in admin
visit users_path
end
it { should have_link('delete', href: user_path(User.first)) }
it "should be able to delete another user" do
expect do
click_link('delete', match: :first)
end.to change(User, :count).by(-1)
end
it { should_not have_link('delete', href: user_path(admin)) }
end
end
subject { page }
describe "index" do
let(:user) { FactoryGirl.create(:user) } #First User Created Here
before do
sign_in user
visit users_path
end
it { should have_title('All users') }
it { should have_content('All users') }
describe "pagination" do
.
.
.
end
describe "delete links" do
it { should_not have_link('delete') }
describe "as an admin user" do
let(:admin) { FactoryGirl.create(:admin) } #Admin Created here
before do
sign_in admin
visit users_path
end
it { should have_link('delete', href: user_path(User.first)) }
it "should be able to delete another user" do
expect do
click_link('delete', match: :first)
end.to change(User, :count).by(-1)
end
it { should_not have_link('delete', href: user_path(admin)) }
end
end
end