对于更高级的 Rails 开发人员来说,这是一个简单的问题,但我无法确定答案。我有两个非常基本的控制器规格没有通过。我没有关联的视图(我正在通过 添加/删除产品RailsAdmin
)但这不应该是一个问题,因为在控制器中我在每个操作中都进行了重定向。
下面附上相关代码。非常感谢您的帮助。谢谢!
products_controller_spec.rb 需要'spec_helper'
describe ProductsController do
describe 'GET #new' do
it "creates a new product" do
get :new
response.should be_success
end
end
describe 'POST #create' do
it "creates a new product and saves it" do
expect{
post :create, product: FactoryGirl.attributes_for(:product)
}.to change(Product, :count).by(1)
end
end
end
控制器/products.rb
class ProductsController < ApplicationController
def new
@product = Product.new
redirect_to 'home'
end
def create
@product = Product.create(params[:product])
redirect_to 'home'
end
end
错误信息:
Failures:
1) ProductsController GET #new creates a new product
Failure/Error: response.should be_success
expected success? to return true, got false
# ./spec/controllers/products_controller_spec.rb:7:in `block (3 levels) in <top (required)>'
2) ProductsController POST #create creates a new product and saves it
Failure/Error: expect{
count should have been changed by 1, but was changed by 0
# ./spec/controllers/products_controller_spec.rb:13:in `block (3 levels) in <top (required)>'
Finished in 0.07747 seconds
2 examples, 2 failures