Help Getting More Info Out of Rspec
I am getting the following failure from one of my tests.
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)>'
I have double checked all of my validations on the model class and cannot find Any reason why this might be failing. Is there a way to get more information logged out from Rspec, or does anyone clearly see where i've gone wrong here?
Thank you for your time. Below is the source for my factories and tests:
# 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_开发者_Go百科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
Edit: added the Instrument controller per apneadiving's request:
# 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
Are you sure that your factory is building a valid instrument? Try adding
Factory.build(:instrument).should be_valid
To your specs.
精彩评论