Ruby on rails RSpec场景示例是否只能有一个断言?
更新> 我混淆了这里的语法,“scenario”实际上是与“it”等价的示例,包含多个断言。原始问题假设“expect”语句作为单个示例运行,而实际上它们是“scenario”示例的多个断言。完整解释见答案 ====================================================================Ruby on rails RSpec场景示例是否只能有一个断言?,ruby-on-rails,ruby,ruby-on-rails-4,rspec,rspec-rails,Ruby On Rails,Ruby,Ruby On Rails 4,Rspec,Rspec Rails,更新> 我混淆了这里的语法,“scenario”实际上是与“it”等价的示例,包含多个断言。原始问题假设“expect”语句作为单个示例运行,而实际上它们是“scenario”示例的多个断言。完整解释见答案 ==================================================================== Houdini:menu-mapper Mike$ rspec spec/features StaticPages Visit Home (FAIL
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
我正在尝试为主页配置一个带有一些简单特性规范的基本原型。Rspec似乎只运行三个示例中的一个。使用rails 4.1.1和rspec 3。已经尝试在spec\u helper.rb
中更改许多配置,包括去掉Spork配置并将其设置回rails默认值。遵循rspec 3文档中的语法指南
有什么想法吗?对于场景规范,每个场景只能有一个断言吗
===========================================
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
/规格/功能/静态页面\u规格.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
========================================
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
/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
Houdini:菜单映射器Mike$rspec规范/功能
静态页面
到家(失败-1)
失败:
1) 我经常回家
失败/错误:期望(第页)有标题(“菜单映射器”)
预期“菜单映射器”包括“菜单映射器”
#./spec/features/static\u pages\u spec.rb:9:in'block(2层)in'
完成时间为0.04204秒(文件加载时间为1.82秒)
1例,1例失败
失败的示例:
rspec./spec/features/static_pages_spec.rb:5#StaticPages访问主页
您的规范(=功能)只有一个示例(=场景)。这个例子有三个期望(=断言)。(您可能需要编辑标题和问题以澄清术语。)当示例中的某个期望失败时,它会引发异常,后续的期望永远不会有机会运行。这就是RSpec的工作方式。(这与你是否使用水豚无关。)如果这种行为掩盖了一个后续的预期会失败,那也没什么大不了的;只要修复第一个并再次运行示例,您就会发现
我建议按照您的方式编写规范。为每个期望编写一个单独的规范会导致更难阅读的规范,因为rspec的DSL并不是设计为高效或清晰地编写每个规范只有一个期望的规范,它会导致更多的示例,这会使测试套件的速度变慢
旁注:水豚的
have_content
和have_text
是相同的,因此您只需要其中一个期望值。如果某个规范中的期望值失败,我认为该规范中不会再运行任何期望值,但我可能记错了——您可以很容易地进行测试。一个规范是否应该有多重期望是有争议的;如果我有一个以上的期望值,我倾向于将期望值保持在较低的水平。谢谢,这就把它清除了。在这种情况下,将多个功能嵌套在一起有意义吗?顺便说一句,在我调试的过程中,我将两者都包括在内,以确保如果我误用了一个功能,我仍然会有两个不同的断言。关于嵌套功能,我不会嵌套功能本身,但我会在功能周围添加上下文,以便它们可以在块之前共享。为了保持可读性,我不希望嵌套超过一个级别;相反,我可能会提取具有很长名称的方法,并从每个需要它们的特性中调用它们。