创建简单的自定义 rpec 匹配器时出错
Error in creating a simple custom rpec matcher
总结 我为一个名为 "one_plus" 的方法创建了 rspec 规范,该方法将任何数字加一。我想制作一个匹配器来检查数字是否等于二。
来源 http://www.reactive.io/tips/2008/12/10/up-and-running-with-custom-rspec-matchers/
错误
john@ubuntu:~/Code/Rspec/Misc/CustomRspecMatchers$ rspec one_plus_spec.rb
F
Failures:
1) the one_plus method should add one to a number
Failure/Error: one_plus(1).should be_two
NoMethodError:
undefined method `two?' for 2:Fixnum
# ./one_plus_spec.rb:10:in `block (2 levels) in <top (required)>'
Finished in 0.00382 seconds
1 example, 1 failure
Failed examples:
rspec ./one_plus_spec.rb:8 # the one_plus method should add one to a number
john@ubuntu:~/Code/Rspec/Misc/CustomRspecMatchers$
Ruby 宝石 Ruby 2.0.0, Rspec 2.14.1
项目
.
├── matchers.rb
├── one_plus.rb
└── one_plus_spec.rb
one_plus_spec.rb
require 'rubygems'
require 'spec'
require_relative 'one_plus'
require_relative 'matchers'
describe 'the one_plus method' do
it 'should add one to a number' do
#one_plus(1).should == 2
one_plus(1).should be_two
#one_plus(0).should_not == 2
one_plus(0).should_not be_two
end
end
one_plus.rb
def one_plus(number)
1 + number
end
matchers.rb
module Matchers
class BeTwo
def matches?(actual)
@actual = actual
@actual == 2
end
def failure_message
"expected 2 but got '#{@actual}'"
end
def negative_failure_message
"expected something else then 2 but got '#{@actual}'"
end
end
def be_two
BeTwo.new
end
alias :equal_two :be_two
end
****编辑 - Rspec 带回溯的命令****
# /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-expectations-2.5.0/lib/rspec/matchers/be.rb:130:in matches?'
# /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-expectations-2.5.0/lib/rspec/expectations/handler.rb:11:in handle_matcher'
# /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-expectations-2.5.0/lib/rspec/expectations/extensions/kernel.rb:27:in should'
# ./one_plus_spec.rb:10:in block (2 levels) in <top (required)>'
# /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-core-2.5.2/lib/rspec/core/example.rb:49:in instance_eval'
# /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-core-2.5.2/lib/rspec/core/example.rb:49:in block (2 levels) in run'
# /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-core-2.5.2/lib/rspec/core/example.rb:106:in with_around_hooks'
# /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-core-2.5.2/lib/rspec/core/example.rb:46:in block in run'
# /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-core-2.5.2/lib/rspec/core/example.rb:99:in block in with_pending_capture'
# /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-core-2.5.2/lib/rspec/core/example.rb:98:in catch'
# /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-core-2.5.2/lib/rspec/core/example.rb:98:in with_pending_capture'
# /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-core-2.5.2/lib/rspec/core/example.rb:45:in run'
# /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-core-2.5.2/lib/rspec/core/example_group.rb:262:in block in run_examples'
# /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-core-2.5.2/lib/rspec/core/example_group.rb:258:in map'
# /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-core-2.5.2/lib/rspec/core/example_group.rb:258:in run_examples'
# /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-core-2.5.2/lib/rspec/core/example_group.rb:232:in run'
# /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-core-2.5.2/lib/rspec/core/command_line.rb:27:in block (2 levels) in run'
# /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-core-2.5.2/lib/rspec/core/command_line.rb:27:in map'
# /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-core-2.5.2/lib/rspec/core/command_line.rb:27:in block in run'
# /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-core-2.5.2/lib/rspec/core/reporter.rb:12:in report'
# /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-core-2.5.2/lib/rspec/core/command_line.rb:24:in run'
# /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-core-2.5.2/lib/rspec/core/runner.rb:55:in run_in_process'
# /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-core-2.5.2/lib/rspec/core/runner.rb:46:in run'
# /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-core-2.5.2/lib/rspec/core/runner.rb:10:in block in autorun'
RSpec 语法启用此:
require 'rspec/expectations'
RSpec::Matchers.define :be_two do
match do |actual|
actual == 2
end
end
据我所知,此语法适用于 RSpec 2.4 到 3.1,可能还有更多版本。
总结 我为一个名为 "one_plus" 的方法创建了 rspec 规范,该方法将任何数字加一。我想制作一个匹配器来检查数字是否等于二。
来源 http://www.reactive.io/tips/2008/12/10/up-and-running-with-custom-rspec-matchers/
错误
john@ubuntu:~/Code/Rspec/Misc/CustomRspecMatchers$ rspec one_plus_spec.rb
F
Failures:
1) the one_plus method should add one to a number
Failure/Error: one_plus(1).should be_two
NoMethodError:
undefined method `two?' for 2:Fixnum
# ./one_plus_spec.rb:10:in `block (2 levels) in <top (required)>'
Finished in 0.00382 seconds
1 example, 1 failure
Failed examples:
rspec ./one_plus_spec.rb:8 # the one_plus method should add one to a number
john@ubuntu:~/Code/Rspec/Misc/CustomRspecMatchers$
Ruby 宝石 Ruby 2.0.0, Rspec 2.14.1
项目
.
├── matchers.rb
├── one_plus.rb
└── one_plus_spec.rb
one_plus_spec.rb
require 'rubygems'
require 'spec'
require_relative 'one_plus'
require_relative 'matchers'
describe 'the one_plus method' do
it 'should add one to a number' do
#one_plus(1).should == 2
one_plus(1).should be_two
#one_plus(0).should_not == 2
one_plus(0).should_not be_two
end
end
one_plus.rb
def one_plus(number)
1 + number
end
matchers.rb
module Matchers
class BeTwo
def matches?(actual)
@actual = actual
@actual == 2
end
def failure_message
"expected 2 but got '#{@actual}'"
end
def negative_failure_message
"expected something else then 2 but got '#{@actual}'"
end
end
def be_two
BeTwo.new
end
alias :equal_two :be_two
end
****编辑 - Rspec 带回溯的命令****
# /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-expectations-2.5.0/lib/rspec/matchers/be.rb:130:in matches?'
# /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-expectations-2.5.0/lib/rspec/expectations/handler.rb:11:in handle_matcher'
# /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-expectations-2.5.0/lib/rspec/expectations/extensions/kernel.rb:27:in should'
# ./one_plus_spec.rb:10:in block (2 levels) in <top (required)>'
# /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-core-2.5.2/lib/rspec/core/example.rb:49:in instance_eval'
# /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-core-2.5.2/lib/rspec/core/example.rb:49:in block (2 levels) in run'
# /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-core-2.5.2/lib/rspec/core/example.rb:106:in with_around_hooks'
# /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-core-2.5.2/lib/rspec/core/example.rb:46:in block in run'
# /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-core-2.5.2/lib/rspec/core/example.rb:99:in block in with_pending_capture'
# /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-core-2.5.2/lib/rspec/core/example.rb:98:in catch'
# /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-core-2.5.2/lib/rspec/core/example.rb:98:in with_pending_capture'
# /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-core-2.5.2/lib/rspec/core/example.rb:45:in run'
# /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-core-2.5.2/lib/rspec/core/example_group.rb:262:in block in run_examples'
# /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-core-2.5.2/lib/rspec/core/example_group.rb:258:in map'
# /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-core-2.5.2/lib/rspec/core/example_group.rb:258:in run_examples'
# /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-core-2.5.2/lib/rspec/core/example_group.rb:232:in run'
# /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-core-2.5.2/lib/rspec/core/command_line.rb:27:in block (2 levels) in run'
# /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-core-2.5.2/lib/rspec/core/command_line.rb:27:in map'
# /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-core-2.5.2/lib/rspec/core/command_line.rb:27:in block in run'
# /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-core-2.5.2/lib/rspec/core/reporter.rb:12:in report'
# /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-core-2.5.2/lib/rspec/core/command_line.rb:24:in run'
# /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-core-2.5.2/lib/rspec/core/runner.rb:55:in run_in_process'
# /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-core-2.5.2/lib/rspec/core/runner.rb:46:in run'
# /home/john/.rvm/gems/ruby-2.0.0-p598/gems/rspec-core-2.5.2/lib/rspec/core/runner.rb:10:in block in autorun'
RSpec 语法启用此:
require 'rspec/expectations'
RSpec::Matchers.define :be_two do
match do |actual|
actual == 2
end
end
据我所知,此语法适用于 RSpec 2.4 到 3.1,可能还有更多版本。