我对rspec测试很陌生。我尝试了以下测试:
require 'spec_helper'
describe "CategoriesController" do
describe "#index" do
context "when signed in" do
it "should have the content 'Sign in'" do
visit categories_path
expect(page).to have_content('Sign in')
end
end
context "when signed in" do
it "should not have the content 'Sign in'" do
visit categories_path
expect(page).not_to have_content('Sign in')
end
end
end
end
现在,我将添加一些身份验证,但我只想让一个测试通过,另一个测试失败。目前两者都失败了,即使它们是相同的,除了。to和。not_to
你知道我做错了什么吗?
您的测试看起来应该在Capybara功能规范中,其中测试模拟用户如何与浏览器交互。但是describe "CategoriesController" do
让它看起来像是你真的写了一个控制器规范。
在将capybara添加到Gemfile后,尝试这样重写。
# in spec/features/sessions_spec.rb
require 'spec_helper'
feature "Sessions" do
scenario "when not signed in" do
visit categories_path
expect(page).to have_content('Sign in')
end
scenario "when signed in" do
visit categories_path
expect(page).not_to have_content('Sign in')
end
end
一旦你把它变成一个特性规范,你也可以像这样添加save_and_open_page
:
scenario "when signed in" do
visit categories_path
save_and_open_page
expect(page).not_to have_content('Sign in')
end