【问题标题】:Help Getting More Info Out of Rspec帮助从 Rspec 中获取更多信息
【发布时间】:2011-08-06 18:33:01
【问题描述】:

我的一项测试出现以下故障。

Failures:

  1) InstrumentController POST create with valid params creates a new Instrument
     Failure/Error: expect {
       count should have been changed by 1, but was changed by 0
     # ./spec/controllers/instrument_controller_spec.rb:63:in `block (4 levels) in <top (required)>'

我已经仔细检查了我对模型类的所有验证,但找不到任何可能失败的原因。有没有办法从 Rspec 中获取更多信息,或者有人清楚地看到我在哪里出错了吗?

感谢您的宝贵时间。以下是我的工厂和测试的来源:

# spec/factories.rb
require 'factory_girl'

FactoryGirl.define do
  # Sequences
  sequence :email do |n|
    "email#{n}@factory.com"
  end

  sequence :instrumentmeaningsid do |n|
    n
  end


  # Roles
  factory :admin_role, :class => Role do
    name 'Admin'
  end

  factory :user_role, :class => Role do
    name 'User'
  end


  # Users
  factory :admin_user, :class => User do
    email 'admin@test.com'
    password 'password'
    password_confirmation 'password'
    name 'Andy McAdmin'
    roles { |a| [a.association(:admin_role)] }
  end

  factory :user, :class => User do
    email
    password 'password'
    password_confirmation 'password'
    name 'Yuri Userington'
    roles { |a| [a.association(:user_role)] } #many to many
  end


  # Instruments
  factory :instrument, :class => Instrument do
    title "Doobie Doo Instrument Title"
    is_valid true
    association :user
    instrumentmeaningsid
  end

end

.

# spec/controllers/instruments_controller_spec.rb
describe InstrumentsController do

  describe "POST create" do

    describe "with valid params" do
      it "creates a new Instrument" do
        expect {
          post :create, :instrument => Factory.build(:instrument, :user => Factory(:user)).attributes
        }.to change(Instrument, :count).by(1)
      end
    end

   end

end

编辑:根据 apneadiving 的要求添加仪器控制器:

# app/controllers/instruments_controller.rb
class InstrumentsController < ApplicationController
  # CanCan
  load_and_authorize_resource

  # GET /instruments
  # GET /instruments.json
  def index
    @instruments = Instrument.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @instruments }
    end
  end

  # GET /instruments/new
  # GET /instruments/new.json
  def new
    @instrument = Instrument.new

    respond_to do |format|
      format.html # new.html.erb
      format.json
    end
  end

  # GET /instruments/1/edit
  def edit
    @instrument = Instrument.find(params[:id])
  end

  # POST /instruments
  # POST /instruments.json
  def create
    @instrument = Instrument.new(params[:instrument])
    @instrument.user = current_user

    respond_to do |format|
      if @instrument.save
        format.html { redirect_to @instrument, notice: 'Instrument was successfully created.' }
        format.json { render json: @instrument, status: :created, location: @instrument }
      else
        format.html { render action: "new" }
        format.json { render json: @instrument.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /instruments/1
  # PUT /instruments/1.json
  def update
    @instrument = Instrument.find(params[:id])

    respond_to do |format|
      if @instrument.update_attributes(params[:instrument])
        format.html { redirect_to @instrument, notice: 'Instrument was successfully updated.' }
        format.json { head :ok }
      else
        format.html { render action: "edit" }
        format.json { render json: @instrument.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /instruments/1
  # DELETE /instruments/1.json
  def destroy
    @instrument = Instrument.find(params[:id])
    @instrument.destroy

    respond_to do |format|
      format.html { redirect_to instruments_url }
      format.json { head :ok }
    end
  end
end

【问题讨论】:

  • 您能否发布失败的规范的实际内容,以及您的模型适用的验证代码?
  • 你应该发布你的歌曲规格。
  • 对不起,伙计们。我不小心复制了错误的失败输出。故障已更新(这与仪器有关)。规范的源代码位于工厂源代码下。

标签: ruby-on-rails ruby testing rspec factory-bot


【解决方案1】:

您确定您的工厂正在制造有效的仪器吗?尝试添加

Factory.build(:instrument).should be_valid

根据您的规格。

【讨论】:

    猜你喜欢
    • 2017-10-08
    • 1970-01-01
    • 1970-01-01
    • 2011-01-05
    • 2012-01-04
    • 1970-01-01
    • 2011-09-07
    • 1970-01-01
    • 2023-03-23
    相关资源
    最近更新 更多