2015-07-13 3 views
0

Я написал несколько тестов для простого приложения. У меня проблема с методом #destroy в моем author_controller. Как я это сделал из некоторых руководств (многие источники показывают подобный подход) Я предполагаю, что это должно работать, но возникает такая ошибка:Ruby on rails rspec destroy count failed

Failure/Error: expect { delete :destroy, id: author.id }.to change(Author, :count).by(-1) expected #count to have changed by -1, but was changed by 0

Вот мой код:

author_controller_spec.rb

require 'rails_helper'       

describe AuthorsController do     
    let(:author) { FactoryGirl.create(:author) } 

    describe 'DELETE #destroy' do                    
    it 'deletes author' do              
     expect { delete :destroy, id: author.id }.to change(Author, :count).by(-1) 
    end                   
    end                   
end                    

authors_controller.rb

class AuthorsController < ApplicationController 
def show 
@author = Author.find(params[:id]) 
end 

def new 
    @author = Author.new 
end 

def create 
    @author = Author.new(author_params) 
    if @author.save 
    redirect_to @author 
    else 
    render 'new' 
    end 
end 

def edit 
    @author = Author.find(params[:id]) 
end 

def update 
    @author = Author.find(params[:id]) 

    if @author.update(author_params) 
    redirect_to @author 
    else 
    render 'edit' 
    end 
end 

def destroy 
    @author = Author.find(params[:id]) 
    @author.books.each do |book| 
    book.destroy if book.authors.count == 1 
    end 
    @author.destroy 
    redirect_to authors_path 
end 

def index 
    @author = Author.all 
end 

private 

    def author_params 
    params.require(:author).permit(:name, :surname, book_ids: []) 
    end 
end 

ответ

1

Вызов let не производится до первого упоминания переменной, так как это ленивая оценка. Это означает, что в вашем expect блоке, вы оба создавать и уничтожать записи, что приводит к общему изменению 0.

Либо создать author вне блока:

describe AuthorsController do     
    let(:author) { FactoryGirl.create(:author) } 

    describe 'DELETE #destroy' do 
    author                   
    it 'deletes author' do              
     expect { delete :destroy, id: author.id }.to change(Author, :count).by(-1) 
    end                   
    end                   
end 

Или, скажите let чтобы не оценивать лениво, используя let!:

describe AuthorsController do     
    let!(:author) { FactoryGirl.create(:author) } 

    describe 'DELETE #destroy' do                    
    it 'deletes author' do              
     expect { delete :destroy, id: author.id }.to change(Author, :count).by(-1) 
    end                   
    end                   
end 
+0

А, спасибо за ответ и объяснение. Я нашел такое решение, и оно сработало, но просто не знало, почему это так. – Hedselu

+0

Нет проблем. Если это работает, пожалуйста, отметьте его как принятый ответ, чтобы следующий человек знал. –

+0

Пришлось ждать - наверное, у многих парней в моем офисе есть стек :) – Hedselu

Смежные вопросы