2013-03-07 63 views
1

檢查字符串中子字符串的包含。TypeError:錯誤參數類型嘗試使用rspec時字符串(預期模塊)

我想我使用正確的語法記錄​​,但它不適合我。我錯過了什麼?

>> require 'rspec-expectations' 
=> true 
>> s = 'potato' 
=> "potato" 
>> s.include?('tat') 
=> true 
>> s.should include('tat') 
TypeError: wrong argument type String (expected Module) 
    from (irb):4:in `include' 
    from (irb):4 
    from /usr/bin/irb:12:in `<main>' 

回答

3

是否應期待一個匹配對象,以便回答你的問題簡單的方法是:

>> require 'rspec-expectations' 
=> true 
>> s = 'potato' 
=> "potato" 
>> s.should RSpec::Matchers::BuiltIn::Include.new('tat') 
=> true 

講起不同匹配一會兒EQ(因爲有一些是關於包括

>> require 'rspec-expectations' 
=> true 
>> s = 'potato' 
=> "potato" 
>> s.should eq('potato') 
NoMethodError: undefined method `eq' for main:Object 

得到eq工作,我們可以包括RSpec::Matchers模塊(方法定義爲線193開始)

>> require 'rspec-expectations' 
=> true 
>> s = 'potato' 
=> "potato" 
>> include RSpec::Matchers 
>> s.should eq('potato') 
=> true 

那麼,你缺少的是延長使用RSpec ::匹配器模塊方法,對象或簡單地傳遞一個匹配器應方式。

問題與包括匹配在IRB仍然存在(不是100%確定爲什麼):

>> require 'rspec-expectations' 
=> true 
>> s = 'potato' 
=> "potato" 
>> include RSpec::Matchers 
>> s.should include('tat') 
TypeError: wrong argument type String (expected Module) 

它可能有一些做中的主對象的環境中工作:

>> self 
=> main 
>> self.class 
=> Object 
>> Object.respond_to(:include) 
=> false 
>> Object.respond_to(:include, true) #check private and protected methods as well 
=> true 

對象有一個私有方法包括。包含來自RSpec :: Matcher的方法從來沒有機會被調用。如果你把它包裝在一個包含RSpec :: Matchers的類中,everyting應該可以工作。

Rspec的做它與MINITEST ::單位:: TestCase的RSpec::Matchers(線168)

+0

不,似乎不是IRB小故障,因爲它從腳本執行時也會發生。 – wim 2013-03-12 02:55:01

+0

我已經做了一些研究並改變了我的答案。 – Kocur4d 2013-03-12 13:24:23

1

您需要周圍的期望一個contextit塊。

相關問題