首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TypeError:尝试使用rspec时参数类型字符串(应为模块)错误

TypeError:尝试使用rspec时参数类型字符串(应为模块)错误
EN

Stack Overflow用户
提问于 2013-03-07 13:34:51
回答 2查看 5.2K关注 0票数 3

检查字符串中子字符串的包含性。

我认为我使用了正确的语法作为文档化的here,但它对我不起作用。我遗漏了什么?

代码语言:javascript
复制
>> 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>'
EN

回答 2

Stack Overflow用户

发布于 2013-03-08 06:33:26

如果需要一个matcher对象,那么回答你问题的最简单的方法是:

代码语言:javascript
复制
>> require 'rspec-expectations'
=> true
>> s = 'potato'
=> "potato"
>> s.should RSpec::Matchers::BuiltIn::Include.new('tat')
=> true

让我们暂时讨论一下不同的匹配器eq (因为有一些关于包括的东西)

代码语言:javascript
复制
>> require 'rspec-expectations'
=> true
>> s = 'potato'
=> "potato"
>> s.should eq('potato')
NoMethodError: undefined method `eq' for main:Object

为了让eq正常工作,我们可以包含RSpec::Matchers模块(方法定义从第193行开始)

代码语言:javascript
复制
>> require 'rspec-expectations'
=> true
>> s = 'potato'
=> "potato"
>> include RSpec::Matchers
>> s.should eq('potato')
=> true

因此,您缺少的是使用RSpec::matcher模块方法来扩展您的对象,或者简单地将matcher传递给should方法。

IRB中的包含匹配器的问题仍然存在(不是100%确定原因):

代码语言:javascript
复制
>> require 'rspec-expectations'
=> true
>> s = 'potato'
=> "potato"
>> include RSpec::Matchers
>> s.should include('tat')
TypeError: wrong argument type String (expected Module)

它可能与在主对象的上下文中工作有关:

代码语言:javascript
复制
>> 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行)

票数 3
EN

Stack Overflow用户

发布于 2013-06-07 07:37:49

您需要一个contextit代码块来表示期望。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15263862

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档