全て、
モデルのテストに問題があります。name:string、location:string、full_name:string、active:boolean というフィールドを使用する単純な顧客テーブルがあります。次の「#{name} #{location}」を含む非表示フィールドとして full_name フィールドを使用し、full_name フィールドでレコードの一意性をテストしています。
テスト「顧客は一意のフルネームなしでは有効ではありません」は、私が問題を抱えているテストであり、重複レコードの挿入をテストしようとしています。assert !customer.save の前にある bang(!) を削除すると、テストに合格します。これは、テストを実行する前にフィクスチャがテーブルにロードされていないように動作しています。rake test:units を実行しています。サーバーを開発モードで実行してみましたが、スキャフォールディングを使用して 2 つのレコードを挿入しました。
私がテストを台無しにした場所について誰かが私にいくつかのガイダンスを与えることができます!
前もって感謝します
リバック
モデル:
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
テストフィクスチャ customers.yml
one:
name: MyString
location: MyString
full_name: MyString
active: false
two:
name: MyString
location: MyString
full_name: MyString
active: false
general:
name: 'Whoever'
location: 'Any Where'
full_name: ''
active: true
テスト ユニット ヘルパー customer_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