我想在rails中测试文件上传,但我不知道如何做到这一点.
这是控制器代码:
def uploadLicense
#Create the license object
@license = License.create(params[:license])
#Get Session ID
sessid = session[:session_id]
puts "\n\nSession_id:\n#{sessid}\n"
#Generate a random string
chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
newpass = ""
1.upto(5) { |i| newpass << chars[rand(chars.size-1)] }
#Get the original file name
upload=params[:upload]
name = upload['datafile'].original_filename
@license.format = File.extname(name)
#calculate license ID and location
@license.location = './public/licenses/' + sessid + newpass + name
#Save the license file
#Fileupload.save(params[:upload], @license.location)
File.open(@license.location, "wb") { |f| f.write(upload['datafile'].read) }
#Set license …
Run Code Online (Sandbox Code Playgroud) 当有人通过URL发送查询时,我正在编写一个规范来测试mashup_controller的行为.我需要模拟URL中包含的参数,并且我读到post()方法会这样做,但是当我收到错误时:
1) MashupController simulates query
Failure/Error: post :create
NoMethodError:
undefined method `post' for
#<RSpec::Core::ExampleGroup::Nested_1:0x980bc50>
# ./mashup_controller_rspec.rb:9:in `block (2 levels) in <top (required)>'
Finished in 0.20199 seconds 1 example, 1 failure
Failed examples:
rspec ./mashup_controller_rspec.rb:7 # MashupController simulates query
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
require 'spec_helper'
require 'mashup_controller.rb'
describe MashupController do
it "simulates query" do
post :create
end
end
Run Code Online (Sandbox Code Playgroud)
对不起,如果我没有任何意义.我对rails和rspec很新.任何帮助,将不胜感激.谢谢.
我想用rails上传一个真正的字体,但我收到了这个错误:
Encoding::UndefinedConversionError in FontsController#create
"\xE6" from ASCII-8BIT to UTF-8
Run Code Online (Sandbox Code Playgroud)
这是控制器中的代码:
def create
@font = Font.new(params[:font])
upload = params[:upload]
name = upload['datafile'].original_filename
@font.font_type = File.extname(name)
@font.location = './public/fonts/' + name
puts "\n\n---------------------------\n#{upload['datafile'].class}\n-----------------------------\n\n"
File.open(@font.location, "w+") { |f| f.write(upload['datafile'].read) }
#Save the license
@font.save
respond_to do |format|
if @font.save
format.html { redirect_to(@font, :notice => 'Font was successfully created.') }
format.xml { render :xml => @font, :status => :created, :location => @font }
else
format.html { render :action => "new" }
format.xml { render …
Run Code Online (Sandbox Code Playgroud)