1

当使用 factory girl 和 RSpec 来测试我的控制器时,我得到了错误:

Failures:

  1) ProductsController Handling POST for create should redirect to products after success
     Failure/Error: response.should redirect_to(products_path)
        Expected response to be a <:redirect>, but was <200>

根据日志文件,看起来对象已经存在于数据库中(并且不满足控制器中的重定向子句),这引发了两个问题:

  1. 为什么电话没有被嘲笑/存根?
  2. 如果我想访问数据库,解决方法是什么?使用@params = FactoryGirl.build(:product).attributes.except(:id, :created_at, :updated_at)也不行。

任何帮助都是极好的。谢谢你。

测试日志:

Processing by ProductsController#create as HTML
Parameters: {"product"=>{"name"=>"Fruit Mania", "description"=>"The mania that is taking over the planet!", "price"=>"9.99", "inventory"=>"1", "product_image"=>"#<File:0x00
User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
Product Exists (0.1ms)  SELECT 1 AS one FROM "products" WHERE LOWER("products"."name") = LOWER('Fruit Mania') LIMIT 1

products_controller_spec.rb:

describe ProductsController, "Handling POST for create" do
  def sign_in_user
    user = FactoryGirl.create(:user)
    sign_in user, @user
  end

  def do_post
    post :create, :product => @params
  end

  before do
    @product = mock_model(Product, :save=> true)
    Product.stub!(:create).and_return(@product)
    @params = FactoryGirl.attributes_for(:product)
  end

  it "should redirect to products after success" do 
     sign_in_user
     do_post
     pending "need to figure out why redirect fails"
     response.should redirect_to(products_path)
  end
end

工厂:

FactoryGirl.define do
  factory :product do
    name "Fruit Mania"
    description "The mania that is taking over the planet!"
    price "9.99"
    inventory 1
    product_image { File.open(File.join(Rails.root, 'spec', 'support', 'uploads', '1000x450.jpeg')) }
  end
end

控制器:

 def create
    @product = Product.new(params[:product])
    if !@product.valid?
      flash.now[:error] = ("The product was not added: <br><li>" + @product.errors.full_messages.join("<li>")).html_safe
      render "new"
    else
      if @product.save
        redirect_to products_path, :notice => "Product created successfully!"
      else
        flash.now[:error] = ("There were issues adding the product to the database. Please retry")
        render "new"
      end
    end
  end
4

1 回答 1

1

我可以告诉你为什么首先发生数据库调用。

你应该替换这一行:

Product.stub!(:create).and_return(@product)

和:

Product.any_instance.stub!(:save).and_return(true)

您正在对类方法进行存根Product::create,但您确实需要对实例方法进行存根Product#save

我想以下方法也可以:

Product.stub!(:new).and_return(@product)
@product.stub(:save).and_return(true)

由于save您无法通过render给出 HTTP 响应 200 的路径,而不是您期望的 302。

至于它为什么要访问数据库,恐怕我不能说为什么模型已经存在于给定的上下文中。如果你只运行这个单一的测试,它有效吗?尝试查看这个问题以获得更多想法:

为什么 factory_girl 不为我进行交易操作?- 测试后行保留在数据库中

我希望这会有所帮助。

于 2013-01-15T00:44:46.667 回答