2017-04-06 40 views
0

我寫我的應用程序的一些RSpec的測試,並有一個bug,它給我的錯誤:未定義的方法`=」爲#<代理商ID:無,名:無,ip_adress:無>

undefined method ` =' for #<Agency id: nil, name: nil, ip_adress: nil> 

有我的測試:

require 'rails_helper' 
RSpec.describe Agency, type: :model do 
    it "should create the agency if all fields are filled" do 
    expect(FactoryGirl.build(:agency)).to be_valid 
    end 
    it "should fail if name is missing" do 
    expect(FactoryGirl.build(:agency, name: nil)).to_not be_valid 
    end 
    it "should fail if ip_adress is missing" do 
    expect(FactoryGirl.build(:agency, ip_adress: nil)).to_not be_valid 
    end 
    it "should fail if there is a double name in db" do 
    agency = FactoryGirl.create(:agency) 
    expect(FactoryGirl.build(:agency, name: agency.name)).to_not be_valid 
    end 
end 

我的代理模式:

class Agency < ActiveRecord::Base 
    module Agencymod 
    attr_accessor :name, :ip_adress 
    end 
    has_many :users 
    has_many :incidents 
    has_many :field_agency_agencies, dependent: :destroy 
    has_many :field_agencies, through: :field_agency_agencies 

    # # Regexp for the postal code. 
    # cp_regexp = /\A((0[1-9])|([1-8][0-9])|(9[0-8])|(2A)|(2B))[0-9]{3}\z/ 
    # # Regexp for email. 
    # email_regexp = /\A[a-zA-Z0-9._-][email protected][a-z0-9._-]{2,}\.[a-z]{2,4}$\z/ 
    # # Regexp for phone number. 
    # phone_regexp = /\A(0|\+33|0033)[1-9][0-9]{8}\z/ 
    # # Regexp for ip address. 
    ip_regexp = /\A(?:(?:[1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}(?:[1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\z/ 

    validates :name, presence: true, 
        uniqueness: { case_sensitive: false }, length: { in: 0..44 } 
    validates :ip_adress, presence: true, format: { with: ip_regexp }, length: { in: 0..49 } 
end 

最後我廠:

factory :agency, class: Agency do |f| 
    f.name  { Faker::Address.city } 
    f.ip_adress "8.8.8.8" 
    end 

這是第一次遇到這種錯誤出現,當我已經測試用戶模型它工作得很好......

對不起,我的英文不好:)

+2

該錯誤發生在哪一行?請顯示錯誤的堆棧跟蹤。 – spickermann

+0

在模塊** Agencymod **下具有'attr_accessor:name,:ip_adress'的任何具體原因。我認爲從模塊中取出應該可以解決你的問題。 –

回答

0

感謝大家發佈的答案。 我發現錯誤它出現在那裏是f.name之間的空間{ Faker::Address.city }

當我已刪除了它,它告訴我同樣的錯誤,但與undefined method'name='...所以我所著的工廠那樣:

f.name Faker::Address.city 

和它工作得很好...... 我的其他工廠如下創作的:

factory :user do |f| 
    f.surname { Faker::Name.first_name } 
    f.name { Faker::Name.last_name } 
    f.pseudo { Faker::Internet.user_name } 
    f.password "password" 
    f.email { Faker::Internet.free_email } 
    f.type_user_id 23 
    f.agency_id 2 
    f.tel "0606060606" 
    f.ip_addr { Faker::Internet.ip_v4_address } 
    end 

和太工作以及..

f.name "TEST" # That works ! 
f.name"TEST" # Works too 
f.name Faker::Address.city # Works 
f.name { Faker::Address.city } # Nope 

問題在寶石?

+0

這對我來說毫無意義。 [動態屬性](https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md#dynamic-attributes)得到了工廠女孩的支持,我沒有理由認爲這會失敗。您是否使用舊版本的圖書館?如果你刪除了'f'變量,如文檔顯示那樣會發生什麼? –

+0

此外,刪除這樣的空白應該*絕對*沒有區別 - 所以我猜你實際上忘了保存文件最初,並運行一箇舊的(不完整)版本的工廠定義。 –

相關問題