gpt4 book ai didi

ruby-on-rails - 类型错误 : wrong argument type String (expected Module)

转载 作者:数据小太阳 更新时间:2023-10-29 07:07:36 24 4
gpt4 key购买 nike

我有以下代码:

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'

这里有什么问题?

最佳答案

首先,你的its语法是错误的,你应该use the block form :

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

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

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.

您可以明确说明主题,一切都会奏效:

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

关于ruby-on-rails - 类型错误 : wrong argument type String (expected Module),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8385025/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com