RSpec场景示例只能有一个断言吗?

时间:2014-07-14 18:44:24

标签: ruby-on-rails ruby ruby-on-rails-4 rspec rspec-rails

更新>

我在这里混淆了语法和'场景'实际上是等同于' it'的示例,包含多个断言。最初的问题假设“期待”和“语句作为单独的示例运行,而它们实际上是对“情景”的多个断言。例。请参阅答案以获得完整的解释。

=============================================== =====================

我尝试使用主页的一些简单功能规格来配置基本原型。 Rspec似乎不会在三个例子中运行多于一个。使用rails 4.1.1和rspec 3.尝试更改spec_helper.rb中的许多配置,包括删除Spork配置并将其设置回rails默认值。遵循rspec 3 docs https://www.relishapp.com/rspec/rspec-rails/v/3-0/docs/feature-specs/feature-spec

的语法指南

有什么想法吗?每种场景规范只能有一个断言吗?

===========================================

/spec/features/static_pages_spec.rb

require 'rails_helper'

feature "StaticPages" do

    scenario "Visit Home" do
        visit root_path

        expect(page).to have_text("Menu Mapper")
        expect(page).to have_title("Menu Mapper")
        expect(page).to have_content("Menu Mapper")
    end
end

========================================

/spec/helpers/spec_helper.rb

require 'rubygems'
require 'spork'

Spork.prefork do
  ENV["RAILS_ENV"] ||= 'test'
  require File.expand_path("../../config/environment", __FILE__)
  require 'rspec/rails'
  #require 'rspec/autorun'    

  Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
  ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)    

  RSpec.configure do |config|

    config.use_transactional_fixtures = true
    config.include FactoryGirl::Syntax::Methods
    config.fail_fast=false      

    config.run_all_when_everything_filtered = true

    if config.files_to_run.one?
      # Use the documentation formatter for detailed output,
      # unless a formatter has already been configured
      # (e.g. via a command-line flag).
      config.default_formatter = 'doc'
    end  

    # config.order = :random

    # Seed global randomization in this process using the `--seed` CLI option.
    # Setting this allows you to use `--seed` to deterministically reproduce
    # test failures related to randomization by passing the same `--seed` value
    # as the one that triggered the failure.
    Kernel.srand config.seed

    # rspec-expectations config goes here. You can use an alternate
    # assertion/expectation library such as wrong or the stdlib/minitest
    # assertions if you prefer.

    config.expect_with :rspec do |expectations|
      # Enable only the newer, non-monkey-patching expect syntax.
      # For more details, see:
      #   - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
      expectations.syntax = :expect
    end

    # rspec-mocks config goes here. You can use an alternate test double
    # library (such as bogus or mocha) by changing the `mock_with` option here.

    config.mock_with :rspec do |mocks|
    # Enable only the newer, non-monkey-patching expect syntax.
    # For more details, see:
    #   - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/

    mocks.syntax = :expect

      # Prevents you from mocking or stubbing a method that does not exist on
      # a real object. This is generally recommended.

      mocks.verify_partial_doubles = true
    end
  end
end

========================================

Houdini:menu-mapper Mike$ rspec spec/features

StaticPages
  Visit Home (FAILED - 1)

Failures:

  1) StaticPages Visit Home
     Failure/Error: expect(page).to have_title("Menu Mapper")
       expected "MenuMapper" to include "Menu Mapper"
     # ./spec/features/static_pages_spec.rb:9:in `block (2 levels) in <top (required)>'

Finished in 0.04204 seconds (files took 1.82 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/features/static_pages_spec.rb:5 # StaticPages Visit Home

1 个答案:

答案 0 :(得分:2)

您的规范(=要素)只有一个示例(=方案)。这个例子有三个期望(=断言)。 (您可能希望编辑标题和问题以澄清术语。)当示例中的期望失败时,它会引发异常,并且随后的期望永远不会有机会运行。这就是RSpec的工作方式。 (这与你是否使用Capybara无关。)如果这种行为隐藏了后续预期会失败的话,这并不是什么大问题。一旦你修复了第一个并再次运行这个例子,你就会发现它。

我建议您按照自己的方式编写规格。为每个期望编写单独的规范会导致难以阅读的规范,因为rspec的DSL不是为了有效或清晰地编写规范,每个规范只有一个期望,而且会产生更多的示例,这会使您的测试套件变慢。

旁注:Capybara的have_contenthave_text是相同的,因此您只需要其中一个期望。