2011-12-05 16 views
5

मैं निम्नलिखित कोड है:लेखन त्रुटि: गलत तर्क प्रकार स्ट्रिंग (उम्मीद मॉड्यूल)

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 

जो मूल रूप से देखने डेटा का भार होता है, श्रेणी के द्वारा बाहर अलग हो गए। इसका उद्देश्य डेटाबेस में प्रत्येक श्रेणी के लिए एक विधि बनाना है। रेल कंसोल के माध्यम से, यह कोड अपेक्षित काम करता है:

[email protected] :002 > ProfileLookup.available_genders 
    ProfileLookup Load (0.6ms) SELECT "profile_lookups".* FROM "profile_lookups" WHERE "profile_lookups"."category" = 'gender' ORDER BY value 
=> ["Female", "Male"] 

हालांकि, मेरी चश्मा विफल हो रही हैं। निम्नलिखित कल्पना:

Exception encountered: #<TypeError: wrong argument type String (expected Module)> 
backtrace: 
/Users/fred/code/my_app/spec/models/profile_lookup_spec.rb:5:in `include' 

क्या समस्या यहाँ है:

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 

के साथ विफल हो?

+0

हमें शामिल-रेखा दिखाएं। जहां भी यह है। –

उत्तर

0

कारणों की एक जोड़ी क्यों यह काम नहीं करता हैं:

  • अंतर्निहित विषयों स्थिर तरीकों के साथ काम करने के लिए प्रकट नहीं होते हैं
  • आप एक संदर्भ, या अपने उदाहरण के आसपास 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 
0

include विधि आमतौर पर कक्षा (या अन्य मॉड्यूल) में मॉड्यूल को शामिल करने के लिए उपयोग की जाती है। ऐसा लगता है कि उन्होंने describe स्कोप में स्कोप में include को ओवरराइट नहीं किया था।

3

सबसे पहले, its के लिए अपने वाक्य रचना गलत है, तो आप चाहिए use the block form:

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

दूसरे, जब आप describe एक वर्ग, विषय आप मिलान कर रहे हैं एक उदाहरण उस वर्ग के है:

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 
+0

+1। यहां तक ​​कि एक वर्णित_क्लास सहायक भी है। तो विषय विषय {description_class} जैसा कुछ हो सकता है। – lucapette

+0

एफवाईआई - मैंने कोशिश की और यह काम नहीं किया। –

संबंधित मुद्दे