检查字符串中子字符串的包含性。
我认为我使用了正确的语法作为文档化的here,但它对我不起作用。我遗漏了什么?
>> 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>'发布于 2013-03-08 06:33:26
如果需要一个matcher对象,那么回答你问题的最简单的方法是:
>> 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::matcher模块方法来扩展您的对象,或者简单地将matcher传递给should方法。
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::Matcher的类中,那么一切都应该可以正常工作。
Rspec使用MiniTest::Unit::TestCase RSpec::Matchers来完成(第168行)
发布于 2013-06-07 07:37:49
您需要一个context或it代码块来表示期望。
https://stackoverflow.com/questions/15263862
复制相似问题