Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rspec - controller test error - Paperclip::AdapterRegistry::NoHandlerError: No handler found for "#<File:0x531beb0>"

I asked about my Rspec test as follow.

Rspec - RuntimeError: Called id for nil, which would mistakenly be 4

On the same code (Rspec test for "items_controller.rb"), I am trying to make the test for "PUT update". However I got the error "Paperclip::AdapterRegistry::NoHandlerError: No handler found for "#".

My Rspec test is as follow. Honestly, I guess that the cause of this fail is ""photo" => File.new(Rails.root + 'app/assets/images/rails.png')" on "let(:valid_attributes)". However, I tried several ways but I couldn't fix. By the way, my rails version is "Rails 3.2.14". Then I tried following post, but also couldn't.

Can't figure out what's causing my tests to fail

The error is as follows.

......F....

Failures:

  1) ItemsController PUT update could not update successfully
     Failure/Error: put :update, {:id => item.to_param, :item => valid_attributes}, valid_session
     Paperclip::AdapterRegistry::NoHandlerError:
     No handler found for "#<File:0x5d4c548>"
     # ./app/controllers/items_controller.rb:110:in `block in update'
     # ./app/controllers/items_controller.rb:108:in `update'
     # ./spec/controllers/items_controller_spec.rb:95:in `block (3 levels) in <top (required)>'

 Finished in 1.75 seconds
 11 examples, 1 failure

 Failed examples:

 rspec ./spec/controllers/items_controller_spec.rb:91 # ItemsController PUT update could not update successfully

 Randomized with seed 40912

My Rspec test is as follows.

require 'spec_helper'
require 'date'

describe ItemsController do

  let(:valid_attributes) { {
    "days" => "1",
    "hours" => "1",
    "minutes" => "1",
    "name"=>"HogeHoge" ,
    "category" => "Gift",
    "min_bid_price" => "100.0",
    "description" => "HogeHoge",
    "photo" => File.new(Rails.root + 'app/assets/images/rails.png')
   } }

  let(:valid_session) { {} }

  it "returns http success" do
    get "index"
    response.should be_success
  end
  it "returns http success" do
    get "new"
    response.should be_success
  end

  describe "POST create" do
    it "" do
    #declare the objects and stubs
    current_user = User.new(id:'1')
    current_user.save
    #"current_user=(user)" function on controller
    controller.current_user = current_user
    #auction
    auction = Auction.new(id:'1',highest_bid_id:'1', extend_bit:'1')
    auction.save
    Auction.stub(:find_by_id).and_return(auction)
    #bid
    bid = Bid.new(auction_id:'1',amount:'150.0')
    bid.save
    Bid.stub(:find_by_id).and_return(bid)
    #item
    item = Item.new(id:'1',auction_id:'1',min_bid_price:'100.0')
    item.save
    Item.stub(:find_by_id).and_return(item)
    date = DateTime.now
    post :create, {:item => {'id' => '2','days'=>'1','hours'=>'1','minutes'=>'1','created_at'=>date}}
    response.should be_success
   end
  end

 describe "GET index" do
  it "assigns all items as @items" do
  item = Item.create! valid_attributes
  get :index, {}, valid_session
  assigns(:items).should eq([item])
 end
end
describe "GET show" do
  it "assigns the requested item as @item" do
   item = Item.create! valid_attributes
   get :show, {:id => item.to_param}, valid_session
   assigns(:item).should eq(item)
  end
end

describe "GET new" do
 it "assigns a new item as @item" do
   get :new, {}, valid_session
   assigns(:item).should be_a_new(Item)
 end
end

describe "GET edit" do
 it "assigns the requested item as @item" do
   item = Item.create! valid_attributes
   get :edit, {:id => item.to_param}, valid_session
   assigns(:item).should eq(item)
 end
end

describe "PUT update" do
  it "could not update successfully" do
    item = Item.create! valid_attributes
    # Trigger the behavior that occurs when invalid params are submitted
    Item.any_instance.stub(:save).and_return(false)
    put :update, {:id => item.to_param, :item => valid_attributes}, valid_session
    assigns(:item).should eq(item)
    response.should redirect_to(@item)
  end

  it "could not update successfully" do
    item = Item.create! valid_attributes
    # Trigger the behavior that occurs when invalid params are submitted
    Item.any_instance.stub(:save).and_return(false)
    put :update, {:id => item.to_param, :item => { }}, valid_session
    response.should render_template("edit")
  end
end

describe "DELETE destroy" do
 it "destroys the requested item" do
   item = Item.create! valid_attributes
   expect {
    delete :destroy, {:id => item.to_param}, valid_session
   }.to change(Item, :count).by(-1)
 end

  it "redirects to the items list" do
   item = Item.create! valid_attributes
   delete :destroy, {:id => item.to_param}, valid_session
   response.should redirect_to(items_url)
  end
 end
end

My "items_controller.rb" is as follw.

require 'timers'

class ItemsController < ApplicationController

  #instance of current user
  def current_user=(user)
   @current_user ||= user
  end

 def extendtimer
   Auction.find_by_id(@auction_id).update_attributes(:extend_bit => 0)
   @exp = Auction.find_by_id(@auction_id).exp_time + 2.minutes
   Auction.find_by_id(@auction_id).update_attributes(:exp_time => @exp)
   @min = Item.find_by_id(@item_id).minutes + 2
   Item.find_by_id(@item_id).update_attributes(:minutes => @min)
   @timer2 = Timers.new
   @extend_timer = @timer2.after(120){ buy }
   @timer2.wait
 end

 def buy
   if Auction.find_by_id(@auction_id).extend_bit == 1
     extendtimer
   else
     if Auction.find_by_id(@auction_id).highest_bid_id != 0
       Item.find_by_auction_id(@auction_id).update_attributes(:sold => 1, :sold_to =>       Bid.find_by_id(Auction.find_by_id(@auction_id).highest_bid_id).user_id )
       MyMailer.auction_winner_email(Auction.find_by_id(@auction_id)).deliver
     else
       Item.find_by_auction_id(@auction_id).update_attributes(:sold => 0, :sold_to => 0    )
       MyMailer.no_bids_email(Auction.find_by_id(@auction_id)).deliver
     end
     @t1.join
   end
 end

 def index
   @items = Item.all

   respond_to do |format|
     format.html # index.html.erb
     format.json { render json: @items }
  end
 end

 # GET /items/1
 # GET /items/1.json
 def show
   @item = Item.find(params[:id])
   respond_to do |format|
     #format.html # show.html.erb
     format.html { render layout: (request.headers["X-Requested-With"] != 'XMLHttpRequest') }
     format.json { render json: @item }
   end
 end

 # GET /items/new
 # GET /items/new.json
 def new
   @item = Item.new
   respond_to do |format|
     format.html # new.html.erb
     format.json { render json: @item }
   end
 end

 # GET /items/1/edit
 def edit
   @item = Item.find(params[:id])
 end

 # POST /items
 # POST /items.json
 def create
   @item = Item.new(params[:item])
   @item.user_id = current_user.id
   respond_to do |format|
     if @item.save
       format.html { redirect_to @item, notice: 'Item was successfully created.' }
       format.json { render json: @item, status: :created, location: @item }
     else
       format.html { render action: "new" }
       format.json { render json: @item.errors, status: :unprocessable_entity }
     end
   end
   @elapsed_seconds =(((params[:item][:days].to_i * 24)+params[:item][:hours].to_i)*60+params[:item][:minutes].to_i)*60
   @auction = Auction.create(:item_id => @item.id, :highest_bid_id => 0, :exp_time =>          @item.created_at+ @elapsed_seconds.seconds, :suspend => 0, :user_id => @current_user.id, :extend_bit => 0 )
   @item.update_attributes(:auction_id => @auction.id)
   @item_id = @item.id
   @auction_id = @auction.id
   @t1 = Thread.new{
   @timer = Timers.new
   @bid_timer = @timer.after(@elapsed_seconds){
   if Auction.find_by_id(@auction_id).suspend != 1
       buy
   end
    }
   @timer.wait
  }
 end

 # PUT /items/1
 # PUT /items/1.json
 def update
   @item = Item.find(params[:id])

   respond_to do |format|

   if @item.update_attributes(params[:item])
     format.html { redirect_to @item, notice: 'Item was successfully updated.' }
     format.json { head :no_content }
   else
     format.html { render action: "edit" }
     format.json { render json: @item.errors, status: :unprocessable_entity }
   end
  end
 end

 # DELETE /items/1
 # DELETE /items/1.json
 def destroy
   @item = Item.find(params[:id])
   @item.destroy

   respond_to do |format|
     format.html { redirect_to items_url }
     format.json { head :no_content }
   end
 end 
end

I would like to have someone's advice. Thank you in advance.

like image 441
Tom0728 Avatar asked Nov 25 '13 15:11

Tom0728


1 Answers

Try using Rack::Test::UploadedFile instead of File.new

require 'rack/test'
Rack::Test::UploadedFile.new('fixtures/test_file.png', 'image/png')
like image 175
DiegoSalazar Avatar answered Oct 21 '22 21:10

DiegoSalazar