ruby-on-rails - #<RSpec::ExampleGroups::TheSigninProcess:0x007fddd28a7750> 的未定义方法 `expect'

标签 ruby-on-rails rspec capybara

我有一个非常基本的 RSpec/Capybara Rails 测试写下来,但我一直得到那个未定义的期望方法

我的规范/功能/signin_spec.rb:

  require "rails_helper"
  RSpec.feature "the signin process", :type => :feature do
    before :each do
      User.create(:email => "user@example.com", :password => "password")
    end

    scenario "signs me in" do
      visit "/users/sign_in"
      within("#new_user") do
        fill_in "Email", :with => "user@example.com"
        fill_in "Password", :with => "password"
      end
      click_button "Log in"
      expect(page).to have_content "successfully"
    end
  end

rails_helper:

当您运行 'rails generate rspec:install' 时,此文件会复制到 spec/
  ENV['RAILS_ENV'] ||= 'test'
  require 'spec_helper'
  require File.expand_path('../../config/environment', __FILE__)
  require 'rspec/rails'

  ActiveRecord::Migration.maintain_test_schema!

  RSpec.configure do |config|
    config.include Devise::TestHelpers, type: :controller
    config.fixture_path = "#{::Rails.root}/spec/fixtures"
    config.use_transactional_fixtures = true
    config.infer_spec_type_from_file_location!
  end

规范助手:
    require 'capybara/rspec'
    RSpec.configure do |config|
      config.include Capybara::DSL
      config.expect_with(:rspec) { |c| c.syntax = :should }
      config.expect_with :rspec do |expectations|
        expectations.include_chain_clauses_in_custom_matcher_descriptions = true
      end
      config.mock_with :rspec do |mocks|
        mocks.verify_partial_doubles = true
      end
   =begin
      config.filter_run :focus
      config.run_all_when_everything_filtered = true
      config.disable_monkey_patching!
      if config.files_to_run.one?
        config.default_formatter = 'doc'
      end
      config.profile_examples = 10
      config.order = :random
      Kernel.srand config.seed
    =end
    end

最佳答案

:expect 中添加 spec_helper 消除了错误

config.expect_with :rspec do |c|
    c.syntax = [:should, :expect]
end

https://www.relishapp.com/rspec/rspec-expectations/docs/syntax-configuration#disable-expect-syntax

感谢您的帮助@apneadiving 和 Frederick

关于ruby-on-rails - #<RSpec::ExampleGroups::TheSigninProcess:0x007fddd28a7750> 的未定义方法 `expect',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29427553/

相关文章:

ruby-on-rails - 转义 ruby​​ 字符串中的特殊字符以匹配 Salesforce SOQL 要求

ruby-on-rails - md5 或 urlsafe_base64 或其他用于创建 auth_token

ruby-on-rails - Ruby on Rails 中缺少回车符(样式/EndOfLine)

forms - 如何使用 capybara 检查表单字段是否已正确预填充?

ruby-on-rails - 让 Headless Chrome 与 Capybara 一起工作

ruby-on-rails-3 - 带有jquery.selectize的Capybara集成测试

ruby-on-rails - Bourbon 与 Twitter Bootstrap for Rails 3.1

ruby-on-rails - 使用 Rspec 测试回形针图像样式

ruby-on-rails - zeus rspec 无法包含所需的文件,但单独使用 rspec 就可以了

ruby-on-rails - 带有 capybara 的嵌套形式