Ruby on rails 3.2 Rails 3.2:失败的断言唯一性:true

Ruby on rails 3.2 Rails 3.2:失败的断言唯一性:true,ruby-on-rails-3.2,Ruby On Rails 3.2,全部, 我在测试模型时遇到问题,我有一个简单的customer表,它使用以下字段:name:string、location:string、full_name:string、active:boolean。我正在使用full_name字段作为包含以下“#{name}#{location}”的隐藏字段,并在full_name字段上测试记录的唯一性 测试“没有唯一全名的客户无效”是我遇到问题的测试,我正在尝试测试重复记录的插入。如果我去掉前面的砰砰声!客户。保存测试将通过。这就像在运行测试之前没有将夹具

全部,

我在测试模型时遇到问题,我有一个简单的customer表,它使用以下字段:name:string、location:string、full_name:string、active:boolean。我正在使用full_name字段作为包含以下“#{name}#{location}”的隐藏字段,并在full_name字段上测试记录的唯一性

测试“没有唯一全名的客户无效”是我遇到问题的测试,我正在尝试测试重复记录的插入。如果我去掉前面的砰砰声!客户。保存测试将通过。这就像在运行测试之前没有将夹具加载到表中一样,我正在运行rake测试:units。我曾尝试在开发模式下运行服务器,并使用scaffolding插入了两条记录,第二次插入失败,并报告了一个错误“全名已被获取”,这是预期的行为

谁能给我一些关于我在哪里搞砸了考试的指导

提前谢谢

回退


型号:

class Customer < ActiveRecord::Base
  attr_accessible :active, :full_name, :location, :name

  validates :active, :location, :name, presence: true
  validates :full_name, uniqueness: true              # case I am trying to test

  before_validation :build_full_name

  def build_full_name
    self.full_name = "#{name} #{location}"
  end

end

测试单元助手客户_Test.rb

require 'test_helper'

class CustomerTest < ActiveSupport::TestCase
  # test "the truth" do
  #   assert true 
  # end

  fixtures :customers

  # Test fields have to be present
  test "customer fields must not be empty" do
    customer = Customer.new
    assert customer.invalid?
    assert customer.errors[:name].any?
    assert customer.errors[:location].any?
    assert_equal " ", customer.full_name     # This is processed by a helper function in the model
    assert customer.errors[:active].any?
  end

  # Test full_name field is unique
  test "customer is not valid without a unique full_name" do
    customer = Customer.new(
    name: customers(:general).name,
    location: customers(:general).location,
    full_name: customers(:general).full_name,
    active: customers(:general).active
    )

    assert !customer.save   # this is the line that fails
    assert_equal "has already been taken", customer.errors[:full_name].join(', ')
  end

end
需要“测试助手”
类CustomerTest
我在fixture customers中发现了问题。yml:

我假设夹具将通过模型进行处理,并且在将数据插入测试数据库之前将调用build\u full\u name助手。情况似乎并非如此

我按如下方式更改了夹具,并纠正了问题:

one:
 name: MyString
 location: MyString
 full_name: MyString
 active: false

two:
 name: MyString2         # changed for consistency, was not the problem
 location: MyString2
 full_name: MyString2
 active: false

general:
 name: 'Whoever'
 location: 'Any Where'
 full_name: 'Whoever Any Where'  #Here was the problem
 active: true
one:
 name: MyString
 location: MyString
 full_name: MyString
 active: false

two:
 name: MyString2         # changed for consistency, was not the problem
 location: MyString2
 full_name: MyString2
 active: false

general:
 name: 'Whoever'
 location: 'Any Where'
 full_name: 'Whoever Any Where'  #Here was the problem
 active: true