rspec 的 .should 在 Ruby 2 中失败(在 describe/it 块之外)? [英] rspec's .should fails (outside describe/it block) in Ruby 2?
问题描述
在 Ruby 2 中,使用 gem rspec 2.14.1(Ubuntu 的最新版本),但未安装 Rails,为什么会失败?
In Ruby 2, with gem rspec 2.14.1 (Ubuntu's most recent), with Rails not installed, why does this fail?
require 'rubygems'
require 'rspec'
3 .should == 3
NoMethodError: undefined method `should' for 3:Fixnum
多年来我一直依赖于方便的习语x .should == y
.
I've relied for years on the convenient idiom x .should == y
.
https://www.relishapp.com/rspec/rspec-expectations/v/2-14/docs/syntax-configuration 和https://www.relishapp.com/rspec/rspec-expectations/docs/syntax-configuration 表示默认情况下仍然支持此语法.
https://www.relishapp.com/rspec/rspec-expectations/v/2-14/docs/syntax-configuration and https://www.relishapp.com/rspec/rspec-expectations/docs/syntax-configuration say that this syntax is still supported by default.
在标题中添加了outside describe/it block",因为这似乎是根本原因.
added "outside describe/it block" to title, as that seems to be the root cause.
推荐答案
如果您想在 describe/it
块之外使用它,似乎您必须先启用它,尽管文档说明它默认启用.我假设 默认启用
意味着在规范文件 [source] 仅.例如:
If you want to use it outside a describe/it
block, it seems you'd have to enable it first, although the documentation states it is enabled by default. I assume that by enable by default
means within an spec file [source] only. For instance:
require 'rubygems'
require 'rspec'
RSpec.configure do |config|
config.expect_with :rspec do |c|
c.syntax = :should
end
end
p 3.should == 3 # true
这篇关于rspec 的 .should 在 Ruby 2 中失败(在 describe/it 块之外)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!