帮助从Rspec中获取更多信息

时间:2011-08-06 18:33:01

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

我的一次测试失败了。

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

1 个答案:

答案 0 :(得分:0)

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

Factory.build(:instrument).should be_valid

根据您的规格。