未定义方法`='表示#<agency id:=“”nil,=“”name:=“”nil,=“”ip_adress:=“”nil =“”>

时间:2017-04-06 08:39:11

标签: ruby-on-rails ruby rspec

我在我的应用程序上写了一些rspec测试并且有一个错误,它向我显示错误:

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._-]+@[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

最后我的工厂:

{{1}}

这是第一次出现此错误,当我测试用户模型时,它运行得非常好......

抱歉我的英语不好:)

1 个答案:

答案 0 :(得分: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

宝石问题?