TypeError:错误的参数类型字符串(预期模块)

发布于 2024-12-19 11:35:46 字数 1257 浏览 1 评论 0原文

我有以下代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一绘本一梦想 2024-12-26 11:35:46

首先,您的 its 语法是错误的,您应该 使用块形式

its(:available_genders) { should include("Male") }

其次,当你描述一个类时,你匹配的主题是一个实例该类:

https://www.relishapp.com/rspec/rspec-core/docs /主题/隐式主题

如果最外层示例组的第一个参数是一个类,则该类的实例将通过 subject() 方法公开给每个示例。

你可以明确地说明这个主题,一切都会成功:

require "spec_helper"

describe ProfileLookup do

  subject { ProfileLookup }

  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

Firstly, your syntax for its is wrong, you should use the block form:

its(:available_genders) { should include("Male") }

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:

If the first argument to the outermost example group is a class, an instance of that class is exposed to each example via the subject() method.

You can be explicit about the subject and everything will work:

require "spec_helper"

describe ProfileLookup do

  subject { ProfileLookup }

  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
楠木可依 2024-12-26 11:35:46

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 overwrite include in the describe scope, only in the it scope.

在巴黎塔顶看东京樱花 2024-12-26 11:35:46

这不起作用有几个原因:

  • 隐式主题似乎不适用于静态方法
  • 您的示例周围需要一个上下文或 it 块:

此代码有效:

require "spec_helper"

describe ProfileLookup do

  it 'should know what lookups are available' do
    ProfileLookup.available_genders.should include("Male")
    ProfileLookup.available_age_groups.should include("0-17")
    ProfileLookup.available_interests.should include("Autos & Vehicles")
    ProfileLookup.available_countries.should include("United States")
  end

end

There are a couple of reasons why this doesn't work:

  • Implicit subjects don't appear to work with static methods
  • You need a context, or it block around your examples:

This code works:

require "spec_helper"

describe ProfileLookup do

  it 'should know what lookups are available' do
    ProfileLookup.available_genders.should include("Male")
    ProfileLookup.available_age_groups.should include("0-17")
    ProfileLookup.available_interests.should include("Autos & Vehicles")
    ProfileLookup.available_countries.should include("United States")
  end

end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文