2013-03-11 1 views
2

레일을 처음 사용하고 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

레일스 3.2를 사용하는 경우 File 대신 UploadedFile을 보내보십시오. UploadedFile은 파일 이름과 이니셜 라이저에 content-type을 취합니다.

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

테스트 또는 테스트 도우미에 Rack::Test::Methods을 포함해야 할 수도 있습니다.

+0

예! 고맙습니다! –

0

데이터베이스에 Paperclip 첨부 파일을 추가 했습니까? 예 : 생성 및 마이그레이션 실행? 테스트 데이터베이스 포함?

1

당신은 또한이 같은 Rack::Test::UploadedFile.new에 바로 가기로 fixture_file_upload를 사용할 수 있습니다

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

이것은 나를 위해 일했지만 'test.png'앞의 경로를 제거해야했습니다. – VinniVidiVicci

관련 문제