【问题标题】:Ruby on Rails tutorial: RSpec test failing when refactoring with matcherRuby on Rails 教程:使用匹配器重构时 RSpec 测试失败
【发布时间】:2013-03-29 06:02:29
【问题描述】:

我正在处理Michael Hartl's Rails tutorial 并且在使用匹配器进行重构时无法通过一项 Rspec 测试。

终端输出

Failures:

  1) Authentication login with invalid information
     Failure/Error: it { should have_error_message('Invalid') }
     NoMethodError:
       undefined method `has_error_message?' for #<Capybara::Session>
     # ./spec/requests/authentication_pages_spec.rb:21:in `block (4 levels) in <top (required)>'

spec/support/utilities.rb

RSpec::Matchers.define :have_error_message do |m|
  match do |page|
    page.should have_selector('div.alert.alert-error', text: m)
  end
end

spec/requests/authentication_pages_spec.rb

require 'spec_helper'

describe "Authentication" do 

  subject { page }

  describe "login page" do
    before { visit login_path }

    it { should have_selector('h1',    text: 'Login') }
    it { should have_selector('title', text: 'Login') }
  end

  describe "login" do
    before { visit login_path }

    describe "with invalid information" do
      before { click_button "Login" }

      it { should have_selector('title', text: 'Login') }
      it { should have_error_message('Invalid') }
    end

    describe "after visiting after page" do
      before { click_link "Home" }
      it { should_not have_selector('div.alert.alert-error') }
    end

    describe "with valid information" do
      let(:user) { FactoryGirl.create(:user) }
      before { valid_login(user) }

      it { should have_selector('title', text: user.name) }
      it { should have_link('Profile', href: user_path(user)) }
      it { should have_link('Logout', href: logout_path) }
      it { should_not have_link('Login', href: login_path) }

      describe "followed by logout" do
        before { click_link "Logout" }

        it { should have_link('Login') }
      end
    end
  end
end

为什么它抱怨没有在任何地方定义的has_error_message? 方法?

编辑/添加:

spec/spec_helper.rb

require 'rubygems'
require 'spork'
#uncomment the following line to use spork with the debugger
#require 'spork/ext/ruby-debug'

Spork.prefork do
  # Loading more in this block will cause your tests to run faster. However,
  # if you change any configuration or code from libraries loaded here, you'll
  # need to restart spork for it take effect.
  ENV["RAILS_ENV"] ||= 'test'
  require File.expand_path("../../config/environment", __FILE__)
  require 'rspec/rails'
  require 'rspec/autorun'

  # Requires supporting ruby files with custom matchers and macros, etc,
  # in spec/support/ and its subdirectories.
  Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

  RSpec.configure do |config|
    # ## Mock Framework
    #
    # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
    #
    # config.mock_with :mocha
    # config.mock_with :flexmock
    # config.mock_with :rr
    config.mock_with :rspec

    # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
    config.fixture_path = "#{::Rails.root}/spec/fixtures"

    # If you're not using ActiveRecord, or you'd prefer not to run each of your
    # examples within a transaction, remove the following line or assign false
    # instead of true.
    config.use_transactional_fixtures = true

    # If true, the base class of anonymous controllers will be inferred
    # automatically. This will be the default behavior in future versions of
    # rspec-rails.
    config.infer_base_class_for_anonymous_controllers = false
  end
end

Spork.each_run do
  # This code will be run each time you run your specs.

end

【问题讨论】:

  • 你也可以发布你的spec/spec_helper.rb文件吗?
  • 现在更新了! @StuartM
  • 注意 - 问题被证明是一些复制/粘贴怪异(我有同样的问题) - 请参阅 cmets on accepted answer。

标签: ruby-on-rails ruby ruby-on-rails-3 rspec railstutorial.org


【解决方案1】:

你在require'ing 你的spec/support/utilities.rb 文件吗?您可能需要在 spec/spec_helper.rb 文件中使用类似的内容来实际加载定义自定义匹配器的文件:

Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

更新:如果您使用的是 Spork,您的 utilities.rb 文件可能会被 Spork 服务器缓存,这意味着您需要重新启动您的主 Spork 进程才能使用新的匹配器通过 RSpec(Spork wiki 上的see details)。

Some people 使用这个技巧来强制 Spork 总是重新加载支持文件:

Spork.each_run do
  Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
end

【讨论】:

  • 我的规范文件中似乎确实有该行,所以这不会导致问题,特别是因为我在同一个 utilities.rb 文件中的 valid_login 方法工作得很好。
  • 你能测试一下你的自定义匹配器是否真的被使用了吗?尝试在page.should have_selector... 之前放置一个raise RuntimeError,如果它没有引发错误,则可能表明您的匹配器没有以某种方式正确注册到RSpec
  • 另外,你在写完这个新的匹配器后重启了你的Spork服务器吗?有关与 Spork 相关的另一种可能性,请参阅我对上述答案的更新
  • 你用的是什么版本的水豚?这可能和stackoverflow.com/questions/13573525/… 一样吗?
  • 好吧,出于某种奇怪的原因,我决定从教程站点重新复制这些方法......这次成功了。 has_error_message? 错误会困扰我很长时间。无论如何,非常感谢您的帮助-我将接受这个作为答案。
【解决方案2】:

如果您使用的是 Capybara 或 RSpec 的更高版本(即不是教程中明确指定的版本),如果我没记错这些自定义匹配器的编写方式需要更改(尽管我可能是错的) ) 不过,请尝试从

更改您的方法
RSpec::Matchers.define :have_error_message do |m|
  match do |page|
    page.should have_selector('div.alert.alert-error', text: m)
  end
end

到:

RSpec::Matchers.define :have_error_message do |m|
  match do |page|
    page.has_selector?('div.alert.alert-error', text: m)
  end
end

这将有望使您能够编写如下测试:

it { should have_error_message("Invalid") }
it { should_not have_error_message("Invalid") }

我遇到了类似的问题,我在 this StackOverflow Q&A 中概述过。

【讨论】:

  • 啊,这不是我的问题,因为我使用的是 Capybara v1.1.2,但我相信我会在未来的某个时候升级 gem,所以这对我非常有帮助知道。谢谢!
猜你喜欢
  • 2016-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多