2011-08-06 2 views
0

테스트 중 하나에서 다음과 같은 오류가 발생합니다.도움말 추가 정보 얻기 Rspec 부족

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 '[email protected]' 
    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 

편집 : 당신이 당신의 공장이 유효한 악기를 구축해야

# 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 
+0

실패한 사양의 실제 내용과 모델의 해당 유효성 검사 코드를 게시 할 수 있습니까? –

+0

노래 사양을 게시해야합니다. –

+0

죄송합니다. 실수로 잘못된 오류 출력을 복사했습니다. 오류가 업데이트되었습니다 (이는 Instrument 관련). 스펙의 소스는 출처 아래에 있습니다. –

답변

0

위치 : apneadiving의 요청에 따라 악기 컨트롤러를 추가? 추가해보기

Factory.build(:instrument).should be_valid 

사양에 따라 변경하십시오.