Ruby on rails rspec destroy count失败

时间:2015-07-13 12:20:19

标签: ruby-on-rails rspec destroy

我为简单的应用程序编写了一些测试。我在authors_controller中遇到了#destroy方法的问题。正如我从一些教程中做到的那样(许多来源显示类似的方法)我想它应该可行,但是会发生这样的错误:

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 个答案:

答案 0 :(得分: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