TypeError:错误的参数类型字符串(预期模块)
我有以下代码:
class ProfileLookup < ActiveRecord::Base
class << self
ProfileLookup.select("DISTINCT category").map{|c| c.category}.each do |category|
define_method("available_#{category.pluralize}".to_sym) do
ProfileLookup.where(category: category).order(:value).all.collect{|g| g.value}
end
end
end
end
它基本上包含大量查找数据,按类别拆分。目的是为数据库中的每个类别创建一个方法。通过 Rails 控制台,此代码按预期工作:
ruby-1.9.3@hub :002 > ProfileLookup.available_genders
ProfileLookup Load (0.6ms) SELECT "profile_lookups".* FROM "profile_lookups" WHERE "profile_lookups"."category" = 'gender' ORDER BY value
=> ["Female", "Male"]
但是,我的规格失败了。以下规范:
require "spec_helper"
describe ProfileLookup do
its(:available_genders).should include("Male")
its(:available_age_groups).should include("0-17")
its(:available_interests).should include("Autos & Vehicles")
its(:available_countries).should include("United States")
end
失败并显示:
Exception encountered: #<TypeError: wrong argument type String (expected Module)>
backtrace:
/Users/fred/code/my_app/spec/models/profile_lookup_spec.rb:5:in `include'
这里有什么问题?
I have the following code:
class ProfileLookup < ActiveRecord::Base
class << self
ProfileLookup.select("DISTINCT category").map{|c| c.category}.each do |category|
define_method("available_#{category.pluralize}".to_sym) do
ProfileLookup.where(category: category).order(:value).all.collect{|g| g.value}
end
end
end
end
which basically contains a load of lookup data, split out by category. The aim is to create a method for each category in the database. Via Rails console, this code works as expected:
ruby-1.9.3@hub :002 > ProfileLookup.available_genders
ProfileLookup Load (0.6ms) SELECT "profile_lookups".* FROM "profile_lookups" WHERE "profile_lookups"."category" = 'gender' ORDER BY value
=> ["Female", "Male"]
However, my specs are failing. The following spec:
require "spec_helper"
describe ProfileLookup do
its(:available_genders).should include("Male")
its(:available_age_groups).should include("0-17")
its(:available_interests).should include("Autos & Vehicles")
its(:available_countries).should include("United States")
end
fails with:
Exception encountered: #<TypeError: wrong argument type String (expected Module)>
backtrace:
/Users/fred/code/my_app/spec/models/profile_lookup_spec.rb:5:in `include'
what is the problem here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,您的
its
语法是错误的,您应该 使用块形式:其次,当你
描述
一个类时,你匹配的主题是一个实例该类:https://www.relishapp.com/rspec/rspec-core/docs /主题/隐式主题:
你可以明确地说明这个主题,一切都会成功:
Firstly, your syntax for
its
is wrong, you should use the block form:Secondly, when you
describe
a class, the subject you're matching is an instance of that class:https://www.relishapp.com/rspec/rspec-core/docs/subject/implicit-subject:
You can be explicit about the subject and everything will work:
include
方法通常用于将模块包含到类(或另一个模块)中。看起来他们没有覆盖describe
范围中的include
,仅覆盖it
范围。The
include
method is usually used to include a Module into a Class (or another Module). Looks like they didn't overwriteinclude
in thedescribe
scope, only in theit
scope.这不起作用有几个原因:
it
块:此代码有效:
There are a couple of reasons why this doesn't work:
it
block around your examples:This code works: