我寫我的應用程序的一些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
這是第一次遇到這種錯誤出現,當我已經測試用戶模型它工作得很好......
對不起,我的英文不好:)
該錯誤發生在哪一行?請顯示錯誤的堆棧跟蹤。 – spickermann
在模塊** Agencymod **下具有'attr_accessor:name,:ip_adress'的任何具體原因。我認爲從模塊中取出應該可以解決你的問題。 –