ruby-on-rails - 为什么我的 rspec 测试失败?

标签 ruby-on-rails rspec

我的“POST create”操作有问题。测试成功,当属性有效时,但当属性无效时,玩家也被保存。这很奇怪,因为只有 :invalid_player 可以保存无效属性。例如,如果我将 wins 更改为 -1 或“string”,则保存具有属性 :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

Controller :

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

最佳答案

您没有在创建方法中为模型分配任何属性。您需要执行以下操作(我假设是 Rails 4):

@player = @tournament.players.new(player_params)

#...

private 

def player_params
  params.require(:player).permit(:wins, :loses, :draws)
end

如果没有任何分配,您很可能会回到数据库默认值零,这是有效的。

关于ruby-on-rails - 为什么我的 rspec 测试失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35524312/

相关文章:

ruby-on-rails - 如何在 Rails 3 中测试 nil 或空条件?

ruby-on-rails - Rails PaperClip 拖放多个文件

ruby-on-rails - rspec 模拟 ActionMailer

ruby-on-rails - 为什么我的功能规范不能在 RSpec 中运行?

ruby-on-rails - Rspec 方法创建错误

ruby-on-rails - 我如何优化这些范围

ruby-on-rails - 每当 cron 作业在 Rails 3 中不起作用时

ruby-on-rails - 在 Rails 中测试 Controller 助手重定向

ruby-on-rails - Rspec 期望(实例)接收方法未按预期工作

testing - 我将如何以 BDD 风格在 Rhomobile 中测试 Controller ?