0
我有一個rails 3.2.16應用程序,它有一個模型和控制器來上傳包含客戶詳細信息列表的csv文件。在應用程序本身這工作正常,但我無法讓測試工作。RSpec文件上傳引發標題行'未定義的方法'
我基本上得到,說
undefined method 'first_name,last_name,address_1,address_2,city .... etc.'
錯誤所以它試圖使用CSV文件的第一行作爲一種方法...?
我正在使用的文件顯示如下
規範(註釋掉線顯示,我一路上試過的東西被看見在SO等問題)
it "upload a file with correct properties" do
#include Rack::Test::Methods
# @file = fixture_file_upload(Rails.root.join('spec/fixtures/files/cust-imp-good.csv'), 'text/csv')
@file = Rack::Test::UploadedFile.new(Rails.root.join('spec/fixtures/files/cust-imp-good.csv'), 'text/csv')
post :create, :customer_import => @file
response.should be_success
end
上傳模型
class CustomerImport #< ActiveRecord::Base
extend ActiveModel::Naming
include ActiveModel::Conversion
include ActiveModel::Validations
attr_accessor :file
def initialize(attributes = {})
debugger
attributes.each { |name, value| send("#{name}=", value) }
end
def persisted?
false
end
def save
if imported_customers.map(&:valid?).all?
valid_ids = true
dive_shop_ids = DiveShop.ids_array
discount_level_ids = DiscountLevel.ids_array
imported_customers.each_with_index do |customer, index|
if !dive_shop_ids.include?(customer.dive_shop_id)
errors.add :base, "Row #{index+2}: dive_shop_id #{customer.dive_shop_id} is not valid"
valid_ids = false
end
if !discount_level_ids.include?(customer.discount_level_id)
errors.add :base, "Row #{index+2}: discount_level_id #{customer.discount_level_id} is not valid"
valid_ids = false
end
end
if valid_ids
imported_customers.each(&:save!)
return_val = imported_customers.count
else
false
end
else
imported_customers.each_with_index do |customer, index|
customer.errors.each do |message|
errors.add :base, "Row #{index+2}: #{message}"
end
end
false
end
end
def imported_customers
@imported_customers ||= ImportRecord.load_imported_records("Customer", file)
end
end
從下面顯示的錯誤,我可以看到它在初始化器中失敗。雖然如果我在那裏放置一個調試器,初始化器看起來沒問題。
從調試器輸出內部初始化從rspec的失敗消息
rdb:1 attributes
Rack::Test::UploadedFile:0x0000000b089a98 @content_type="text/csv", @original_filename="cust-imp-good.csv", @tempfile=#<File:/tmp/cust-imp-good.csv20131212-26548-ynutnh>>
rdb:1
輸出
Failures:
1) CustomerImportsController POST 'create' upload a file with correct properties
Failure/Error: post :create, :customer_import => @file
NoMethodError:
undefined method `first_name,last_name,address1,address2,address3,city,state,country,postcode,telephone,email,dob,local_contact,emergency_name,emergency_number,dive_shop_id,discount_level_id
=' for #<CustomerImport:0x0000000a5f7580>
# ./app/models/customer_import.rb:10:in `block in initialize'
# ./app/models/customer_import.rb:10:in `initialize'
# ./app/controllers/customer_imports_controller.rb:14:in `new'
# ./app/controllers/customer_imports_controller.rb:14:in `create'
# ./spec/controllers/customer_imports_controller_spec.rb:20:in `block (3 levels) in <top (required)>'
任何幫助,將不勝感激我試圖在Undefined Method 'NameOfField' for #<Model:0x000...>即耙所示的解決方案:分貝:測試:準備並bundle exec rspec。但這並沒有工作,要麼
EDIT包括控制器代碼
class CustomerImportsController < ApplicationController
before_filter do
@menu_group = "diveshop"
end
def new
@customer_import = CustomerImport.new
end
def create
if params[:customer_import] != nil
@customer_import = CustomerImport.new(params[:customer_import])
return_value = @customer_import.save # need to add @customer_import.file here
if return_value != false
addauditlog("A bulk import of customers was carried out")
redirect_to customers_url, notice: "Imported #{return_value} customers successfully."
else
render :new
end
else
flash[:error] = "You have not selected a file"
redirect_to new_customer_import_url
end
end
end
已添加該代碼。該文件的第一行與錯誤所示完全相同 –