2015-01-03 5 views
0

Резюме Я создал спецификацию rspec для метода под названием one_plus, который добавляет его к любому числу. Я хочу сделать совпадение, которое будет проверять, равно ли число, равное ДВА или нет.Ошибка при создании простого пользовательского rpec-матчи

Источникhttp://www.reactive.io/tips/2008/12/10/up-and-running-with-custom-rspec-matchers/

Ошибка

[email protected]:~/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 
[email protected]:~/Code/Rspec/Misc/CustomRspecMatchers$ 

Рубиновые камни Руби 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 

**** EDIT - РГКП с командой с трассировкой ****

# /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' 

ответ

1

RSpec синтаксис позволяет это:

require 'rspec/expectations' 

RSpec::Matchers.define :be_two do 
    match do |actual| 
    actual == 2 
    end 
end 

Насколько я знаю, этот синтаксис работает для RSpec 2.4 через 3.1, возможно более версии.

+0

Спасибо. Есть ли способ каким-то образом заставить его работать на rspec 2.x? – stack1

+0

Спасибо, это работает. Мне не нужно использовать требование, которое вы мне дали. Однако, я должен удалить линию псевдонима. Как использовать псевдоним с вашим методом? EDIT - я просто поместил линию псевдонима до конца метода определения, и он сработал. Кажется, я где-то это видел. – stack1

+0

Чтобы добавить параметр к методу: be_two Это: be_two (ожидается), не работает. Вы должны сделать это так: RSpec :: Matchers.define: be_two do | expected |. Я добавляю это здесь для моей справки и для других. – stack1

Смежные вопросы