Я получаю следующий сбой в одном из моих тестов.
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
Редактировать: добавлен контроллер инструмента для запроса апноэ:
# 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