无法弄清楚是什么导致我的测试失败

时间:2013-03-11 15:09:33

标签: ruby-on-rails tdd paperclip

我是rails的新手,我在没有做TDD的情况下构建了一个应用程序,但我现在回去尝试通过所有测试。我已经通过了他们中的大部分,但还有一些与我无法弄清楚的相同问题有关。该应用程序也正常运行,我无法通过这些测试。

测试失败并提供:

1) ProductsController POST create with valid params assigns a newly created product as @product
 Failure/Error: post :create, {:product => valid_attributes}, valid_session
 Paperclip::AdapterRegistry::NoHandlerError:
   No handler found for "#<File:0x007fc6d17b28f8>"
 # ./app/controllers/products_controller.rb:43:in `new'
 # ./app/controllers/products_controller.rb:43:in `create'
 # ./spec/controllers/products_controller_spec.rb:86:in `block (4 levels) in <top (required)>'

2) ProductsController POST create with valid params creates a new Product
 Failure/Error: post :create, {:product => valid_attributes}, valid_session
 Paperclip::AdapterRegistry::NoHandlerError:
   No handler found for "#<File:0x007fc6d1757cf0>"
 # ./app/controllers/products_controller.rb:43:in `new'
 # ./app/controllers/products_controller.rb:43:in `create'
 # ./spec/controllers/products_controller_spec.rb:81:in `block (5 levels) in <top (required)>'
 # ./spec/controllers/products_controller_spec.rb:80:in `block (4 levels) in <top (required)>'

3) ProductsController POST create with valid params redirects to the created product
 Failure/Error: post :create, {:product => valid_attributes}, valid_session
 Paperclip::AdapterRegistry::NoHandlerError:
   No handler found for "#<File:0x007fc6d36b3dd8>"
 # ./app/controllers/products_controller.rb:43:in `new'
 # ./app/controllers/products_controller.rb:43:in `create'
 # ./spec/controllers/products_controller_spec.rb:92:in `block (4 levels) in <top (required)>'

&#34;创建&#34;我的控制器中的方法:

def create
  @product = Product.new(params[:product])

  respond_to do |format|
    if @product.save
      format.html { redirect_to admin_path, notice: 'Product was successfully created.' }
      format.json { render json: @product, status: :created, location: @product }
    else
      format.html { render action: "new" }
      format.json { render json: @product.errors, status: :unprocessable_entity }
    end
  end
end

我的模特:

class Product < ActiveRecord::Base
  attr_accessible :designed, :features, :photo, :manufactured, :name, :case_study

  has_attached_file :photo, { 
    :styles => {
      :thumb => "x50>", 
      :small => "x150>", 
      :detail => "x600>"
    }
  }.merge(PAPERCLIP_STORAGE_OPTIONS)

  validates_attachment_presence :photo
  validates_attachment_size :photo, :less_than => 5.megabytes
  validates_attachment_content_type :photo, :content_type => ['image/jpeg', 'image/png']
end

我的测试:

before(:each) do
  @image = File.new(Rails.root + 'spec/fixtures/images/test.png')
end 

def valid_attributes
  { "photo" => @image }
end

describe "POST create" do
describe "with valid params" do
  it "creates a new Product" do
    expect {
      post :create, {:product => valid_attributes}, valid_session
    }.to change(Product, :count).by(1)
  end

  it "assigns a newly created product as @product" do
    post :create, {:product => valid_attributes}, valid_session
    assigns(:product).should be_a(Product)
    assigns(:product).should be_persisted
  end

  it "redirects to the created product" do
    post :create, {:product => valid_attributes}, valid_session
    response.should redirect_to(admin_path)
  end
end
end

3 个答案:

答案 0 :(得分:9)

如果您使用的是Rails 3.2,请尝试在测试中发送UploadedFile代替FileUploadedFile在其初始化程序中采用文件名和内容类型。

before(:each) do
  @image = Rack::Test::UploadedFile.new(Rails.root.join('spec/fixtures/images/test.png'), 'image/png')
end

您可能必须在测试或测试帮助程序中包含Rack::Test::Methods

答案 1 :(得分:1)

您还可以使用fixture_file_upload作为此Rack::Test::UploadedFile.new的快捷方式:

post :create, product: { photo: fixture_file_upload('spec/fixtures/images/test.png', 'image/png') }

答案 2 :(得分:0)

您是否已将Paperclip附件添加到数据库中 - 例如创建和运行迁移?包括测试数据库?

相关问题