我的问题是" POST create"行动。当属性有效时,测试成功通过,但当它们无效时,也会保存播放器。这很奇怪,因为只有:invalid_player,可以用无效的属性保存。例如,如果我改变为-1或者#34;字符串",则保存具有属性:invalid_player的玩家。但是如果我改变:player的属性,比如wins = -1,验证器会阻止玩家被保存。
控制台输出,显示错误消息:
Failures:
1) PlayersController user is signed in POST create with invalid attributes does not save the new player
Failure/Error:
expect{
post :create, { tournament_id: @tournament, player: FactoryGirl.attributes_for(:invalid_player) }
}.to_not change(Player, :count)
expected #count not to have changed, but did change from 1 to 2
# ./spec/controllers/players_controller_spec.rb:111:in `block (5 levels) in <top (required)>'
这是我的播放器型号:
class Player < ActiveRecord::Base
belongs_to :user
belongs_to :tournament
validates :wins, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
validates :loses, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
validates :draws, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
end
玩家的工厂档案:
FactoryGirl.define do
factory :player do
wins 0
loses 0
draws 0
end
factory :invalid_player, parent: :player do
wins -1
loses 0
draws 0
end
end
规格测试:
context "user is signed in" do
before do
@tournament = create(:tournament)
@player = create(:player)
@user = create(:user)
@request.env["devise.mapping"] = Devise.mappings[:user]
sign_in(@user)
controller.stub(:current_user).and_return(@user)
end
describe "GET new" do
end
describe "GET index" do
it "renders the :index view" do
get :index, tournament_id: @tournament
expect(response).to render_template :index
end
end
describe "GET show" do
it "renders the :show view" do
get :show, { id: @player, tournament_id: @tournament }
expect(response).to render_template :show
end
end
describe "POST create" do
context "with valid attributes" do
it "creates a new player" do
expect{
post :create, { tournament_id: @tournament, player: FactoryGirl.attributes_for(:player) }
}.to change(Player,:count).by(1)
end
it "redirects to the tournament" do
post :create, { tournament_id: @tournament, player: FactoryGirl.attributes_for(:player) }
expect(response).to redirect_to @tournament
end
end
context "with invalid attributes" do
it "does not save the new player" do
expect{
post :create, { tournament_id: @tournament, player: FactoryGirl.attributes_for(:invalid_player) }
}.to_not change(Player, :count)
end
it 're-renders the new method' do
post :create, { tournament_id: @tournament, player: FactoryGirl.attributes_for(:invalid_player) }
response.should render_template :new
end
end
end
end
控制器:
class PlayersController < ApplicationController
before_action :set_tournament
before_action :set_admin, only: [:edit, :update, :destroy]
before_action :authenticate_user!, only: [:new, :create, :edit, :update, :destroy]
def index
@players = @tournament.players.all
end
def show
@player = Player.find(params[:id])
end
def new
@player = @tournament.players.new
end
def create
if current_user.player.nil? == false
flash[:error] = "You're already in tournament."
redirect_to tournaments_url
else
@player = @tournament.players.new
@player.user_id = current_user.id
if @player.save
redirect_to @tournament
else
render 'new'
end
end
end
def edit
if current_user == @admin
@player = @tournament.players.find(params[:id])
else
redirect_to tournaments_url
end
end
def update
if current_user == @admin
@player = @tournament.players.find(params[:id])
if @player.update_attributes(game_params)
flash[:success] = "Player was updated successful"
redirect_to @tournament
end
else
redirect_to tournaments_url
end
end
def destroy
@player = Player.find(params[:id])
flash[:success] = "Player deleted"
redirect_to @tournament
end
private
def set_tournament
@tournament = Tournament.find(params[:tournament_id])
end
def set_admin
@tournament = Tournament.find(params[:tournament_id])
@admin = @tournament.user
end
end
答案 0 :(得分:4)
您没有在create方法中为模型指定任何属性。您需要执行以下操作(我假设它是轨道4):
@player = @tournament.players.new(player_params)
#...
private
def player_params
params.require(:player).permit(:wins, :loses, :draws)
end
如果没有任何分配,您很可能会回到数据库默认值零,这是有效的。