2013-03-11 77 views
2

我是新來的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)>' 

「創建」 法在我的控制器:

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 

回答

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

+0

是的!謝謝! – 2013-03-11 17:23:05

0

您是否已將Paperclip附件添加到數據庫 - 例如已創建並運行遷移?包括測試數據庫?

1

您還可以使用fixture_file_upload的快捷方式Rack::Test::UploadedFile.new這樣的:

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

這對我有效,但我不得不刪除'test.png'前面的路徑 – VinniVidiVicci 2016-01-30 14:49:15

相關問題