"的处理程序-6ren"> "的处理程序-我如下询问了我的 Rspec 测试。 Rspec - RuntimeError: Called id for nil, which would mistakenly be 4 在相同的代码上(“ite-6ren">
gpt4 book ai didi

ruby-on-rails - Rspec - Controller 测试错误 - Paperclip::AdapterRegistry::NoHandlerError: 找不到 "#"的处理程序

转载 作者:数据小太阳 更新时间:2023-10-29 07:08:38 28 4
gpt4 key购买 nike

我如下询问了我的 Rspec 测试。

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

在相同的代码上(“items_controller.rb”的 Rspec 测试),我试图对“PUT update”进行测试。但是我收到错误消息“Paperclip::AdapterRegistry::NoHandlerError:找不到“#”的处理程序。

我的 Rspec 测试如下。老实说,我猜这次失败的原因是“let(:valid_attributes)”上的“photo”=> File.new(Rails.root + 'app/assets/images/rails.png')”。但是,我尝试了几种方法,但无法修复。顺便说一下,我的 Rails 版本是“Rails 3.2.14”。然后我尝试关注帖子,但还是不能。

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

错误如下

......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

我的Rspec测试如下。

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

我的“items_controller.rb”如下所示。

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

我想听听别人的建议。提前谢谢你。

最佳答案

尝试使用 Rack::Test::UploadedFile 而不是 File.new

require 'rack/test'
Rack::Test::UploadedFile.new('fixtures/test_file.png', 'image/png')

关于ruby-on-rails - Rspec - Controller 测试错误 - Paperclip::AdapterRegistry::NoHandlerError: 找不到 "#<File:0x531beb0>"的处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20197474/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com