Ruby on rails rspec 销毁计数失败
Ruby on rails rspec destroy count fails
我已经为简单的应用程序编写了一些测试。我的 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
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
我已经为简单的应用程序编写了一些测试。我的 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
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