为什么我的rspec测试失败?

内克隆

我的“ POST创建”操作存在问题。当属性有效时,测试成功通过,但是当属性无效时,也会保存播放器。奇怪的是,只有:invalid_player可以保存无效的属性。例如,如果我更改,则获胜为-1或“字符串”,则保存具有属性: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
BroiSatse

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

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

#...

private 

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

为什么我的rspec测试失败?

来自分类Dev

为什么我的guard-rspec测试失败?

来自分类Dev

为什么我的模型验证失败了,为什么我的rspec测试没有通过?

来自分类Dev

为什么我的rspec测试本身可以通过,但是在运行特定测试时却失败了?

来自分类Dev

为什么我的Rspec post:create无效参数控制器测试失败?

来自分类Dev

为什么我的NUnit测试失败?

来自分类Dev

Python:为什么我的“小于”测试失败?

来自分类Dev

为什么我的NUnit测试不会失败?

来自分类Dev

为什么我失败的Ruby单元测试不会失败?

来自分类Dev

为什么我的Mocha / chai错误抛出测试失败?

来自分类Dev

为什么添加参数后我的单元测试失败?

来自分类Dev

为什么重构时我的单元测试失败

来自分类Dev

不确定为什么我的测试总是失败

来自分类Dev

为什么我的 sinon 测试超时而不是失败?

来自分类Dev

为什么此测试失败-未实现错误Rspec 3.1.x和Rails 4.1.9

来自分类Dev

删除rspec中的测试-change(Model,:count)失败-为什么需要重新加载?

来自分类Dev

使用自己的辅助方法,Rspec测试不应失败,为什么呢?滑轨

来自分类Dev

烧瓶测试-为什么测试失败

来自分类Dev

为什么这些rspec测试“未决”?

来自分类Dev

当单元测试有解析错误时,为什么我的业力测试不会失败?

来自分类Dev

为什么即使噩梦测试失败了,我的噩梦测试也总是成功?

来自分类Dev

为什么我的rspec-rails生成的规范由于路由异常而失败?

来自分类Dev

为什么我的比较失败?

来自分类Dev

为什么我的递归失败了?

来自分类Dev

为什么我的NSMutableURLRequest失败?

来自分类Dev

为什么我的Nginx失败了?

来自分类Dev

Grok RSpec测试失败

来自分类Dev

Rspec,失败测试日期

来自分类Dev

为什么这个Jest测试不会失败?

Related 相关文章

  1. 1

    为什么我的rspec测试失败?

  2. 2

    为什么我的guard-rspec测试失败?

  3. 3

    为什么我的模型验证失败了,为什么我的rspec测试没有通过?

  4. 4

    为什么我的rspec测试本身可以通过,但是在运行特定测试时却失败了?

  5. 5

    为什么我的Rspec post:create无效参数控制器测试失败?

  6. 6

    为什么我的NUnit测试失败?

  7. 7

    Python:为什么我的“小于”测试失败?

  8. 8

    为什么我的NUnit测试不会失败?

  9. 9

    为什么我失败的Ruby单元测试不会失败?

  10. 10

    为什么我的Mocha / chai错误抛出测试失败?

  11. 11

    为什么添加参数后我的单元测试失败?

  12. 12

    为什么重构时我的单元测试失败

  13. 13

    不确定为什么我的测试总是失败

  14. 14

    为什么我的 sinon 测试超时而不是失败?

  15. 15

    为什么此测试失败-未实现错误Rspec 3.1.x和Rails 4.1.9

  16. 16

    删除rspec中的测试-change(Model,:count)失败-为什么需要重新加载?

  17. 17

    使用自己的辅助方法,Rspec测试不应失败,为什么呢?滑轨

  18. 18

    烧瓶测试-为什么测试失败

  19. 19

    为什么这些rspec测试“未决”?

  20. 20

    当单元测试有解析错误时,为什么我的业力测试不会失败?

  21. 21

    为什么即使噩梦测试失败了,我的噩梦测试也总是成功?

  22. 22

    为什么我的rspec-rails生成的规范由于路由异常而失败?

  23. 23

    为什么我的比较失败?

  24. 24

    为什么我的递归失败了?

  25. 25

    为什么我的NSMutableURLRequest失败?

  26. 26

    为什么我的Nginx失败了?

  27. 27

    Grok RSpec测试失败

  28. 28

    Rspec,失败测试日期

  29. 29

    为什么这个Jest测试不会失败?

热门标签

归档