1
module V1
class Player < ActiveRecord::Base
validates :name, presence: true
validates :default_pull_rate, numericality: true, allow_nil: false
has_many :player_links
end
end
規格(我甚至嘗試明確設置default_pull_rate直列如下圖):
it "creates a new player" do
expect { post :create, format: :json, player: FactoryGirl.attributes_for(:player, default_pull_rate: 5) }.to change(V1::Player, :count).by(1)
end
廠:
FactoryGirl.define do
factory :player, class: V1::Player do
name "Frank"
default_pull_rate 100
end
控制器:
....
def create
@player = Player.new(player_params)
if @player.save!
redirect_to @player
end
end
private
def player_params
params.require(:player).permit(:name, :default_pull_rated)
end
錯誤消息:
ActiveRecord::RecordInvalid: Validation failed: Default pull rate is not a number
傳遞模型規格:在player_params
it "is invalid without a default_pull_rate" do
expect(FactoryGirl.build(:player, default_pull_rate: nil)).to_not be_valid
end
it "is invalid when default_pull_rate is a string" do
expect(FactoryGirl.build(:player, default_pull_rate: "fast")).to_not be_valid
end