Ruby on rails 我的两个测试都失败了,尽管它们是相反的(expect(…)。to和expect(…)。not___to)

Ruby on rails 我的两个测试都失败了,尽管它们是相反的(expect(…)。to和expect(…)。not___to),ruby-on-rails,rspec,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_con

我对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之外是相同的


你知道我做错了什么吗?

你的测试看起来应该在水豚特性规范中,测试模拟用户与浏览器的交互方式。但是
描述“CategoriesController”做的
使它看起来像是您实际编写了一个控制器规范

在添加到您的文件后,尝试这样重写

# 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\u和\u open\u page
,如下所示:

  scenario "when signed in" do
    visit categories_path
    save_and_open_page
    expect(page).not_to have_content('Sign in')
  end

失败消息是什么?好的,
expect
API是
.to\u not
和not
。not\u to
…我明白了。我知道水豚,但还不熟悉了解水豚依赖的方法。测试是否应该在Gem丢失而不是运行但失败时触发错误?我不确定在运行其他人编写的规范时,如何区分不同的gem(无论如何在这个阶段),一般来说,在控制器规范中,永远不会依赖水豚。在特性规范中,它始终是依赖的。我建议的修订(即,将规范从控制器规范移动到功能规范)是否有效?是的。事实上,它告诉我save_和open_页面也需要启动gem,我把它添加到了我的gem文件中。在bundle安装之后,一切都很好,我认为save_和open_页面真的会派上用场。谢谢