我的测试都失败了,即使它们形成对比(期望(...)。到和期望(...)。not_to)

时间:2014-03-17 15:20:09

标签: ruby-on-rails rspec

我对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

之外是相同的

任何想法我做错了什么?

1 个答案:

答案 0 :(得分:1)

您的测试看起来应该属于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