原帖的其余部分:
注意:为了使事情更容易阅读,这不是完整的代码
使用'activerecord-import'gem
进行测试./规格/特征/进口商/ importer_property_list_spec.rb
require 'rails_helper'
describe 'import property list data to database' do
puts "Enter test"
before(:each) do
visit 'importers#import_property_list'
end
let!(:first_cash_account) { FactoryGirl.build(:first_cash_account) }
let!(:last_cash_account) { FactoryGirl.build(:last_cash_account) }
let!(:first_entity) { FactoryGirl.build(:first_entity) }
let!(:last_entity) { FactoryGirl.build(:last_entity) }
context 'uploading property list file, causes data to be importet to database' do
it 'creates cash accounts' do
puts "hit first test"
expect(CashAccount.first.code).to eql(first_cash_account.code)
expect(CashAccount.last.code).to eql(last_cash_account.code)
end
it 'creates entities' do
puts "hit second test"
expect(Entity.first.name).to eql(first_entity.name)
expect(Entity.first.cash_account.code).to eql(first_cash_account.code)
expect(Entity.last.name).to eql(last_entity.name)
end
end
end
我的控制器 ./app/controllers/importers_controller.rb
class ImportersController < ApplicationController
def index
end
def show
end
def import_property_list
puts "Enter import"
cash_accounts = []
excel = property_list_excel
(4..excel.last_row).each do |row|
code = eighth_col(excel, row)
cash_account = CashAccount.new(:code => code)
name = eleventh_col(excel, row)
name = remove_non_breaking_space(name)
cash_account.entities.build(:name => name)
cash_accounts << cash_account
end
CashAccount.import cash_accounts, recursive: true, :validates => false
puts "Finish import"
end
end
这是我在控制台中的输出
注意:第一次测试只是因为我的factory_girl工厂协会创建CashAccount记录而通过
答案 0 :(得分:0)
首先,看起来有点不合适的东西:不应该在之前(:每个)块中吗?我通常不会像这样在公开场合留下设置代码。
let!(:first_cash_account) { FactoryGirl.build(:first_cash_account) }
let!(:last_cash_account) { FactoryGirl.build(:last_cash_account) }
let!(:first_entity) { FactoryGirl.build(:first_entity) }
let!(:last_entity) { FactoryGirl.build(:last_entity) }
基本上这是说没有创建实体记录。你在哪里创建实体记录?在这里的块:(4..excel.last_row).each do |row|
但块甚至在运行?看看excel = property_list_excel
中是否有任何合法值 - 请检查excel是否为零?并检查块是否实际循环:
def import_property_list
puts "Enter import"
cash_accounts = []
excel = property_list_excel
(4..excel.last_row).each do |row|
code = eighth_col(excel, row)
puts "IT's ITERATING THROUGH*********************" # <----------------- add this line
cash_account = CashAccount.new(:code => code)
name = eleventh_col(excel, row)
name = remove_non_breaking_space(name)
cash_account.entities.build(:name => name)
cash_accounts << cash_account
end
CashAccount.import cash_accounts, recursive: true, :validates => false
puts "Finish import"
end
end
您确定该链接实际上是通过以下类型的语法访问的:访问&#39; importers#import_property_list&#39; ---&GT;尝试使用指定的路径或适当的URL代替。我不确定您在访问网页时是否可以使用该语法,而且我认为这可能是您的问题。例如尝试import_property_etc_etc_list_named_path -
应该有助于查明问题吗?
答案 1 :(得分:0)
您在let语句中调用了FactoryGirl.build
而不是FactoryGirl.create
。这意味着对象只存在于内存中但实际上并不存在于数据库中,因此当您调用Entity.first
时,它们不是数据库中的实体。
答案 2 :(得分:0)
我在测试中改变了路线
visit 'importers#import_property_list'
到
visit import_property_list_importers_path