Rails 4 rspec 测试在代码 运行 之前失败
Rails 4 rspec test fails before code is run
编辑:我最初的假设是不正确的。似乎我的测试在它完成 运行 所有代码块之前就已经达到了它的断言,但真正发生的是,我是 运行 我的完整测试套件并且我正在从一个击中相同控制器动作的不同测试。我 运行 下面的个人测试发现我什至没有击中控制器。我不得不在测试中更正我的路线。一切就绪。
原始 post 的其余部分:
NOTE: to make things easier to read, this is NOT the complete code
我的测试 'activerecord-import' gem
./spec/features/importer/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 记录
首先,有些东西看起来有点格格不入:这不应该在 before(:each) 块中吗?我通常不会像那样公开设置代码。
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
编辑:- 真正的问题
您确定 link 实际上是通过这种语法访问的:访问 'importers#import_property_list' ---> 尝试使用命名路径或适当的 url 代替。我不确定您在访问网页时是否可以使用该语法,我认为这可能是您的问题。例如试试 import_property_etc_etc_list_named_path –
这应该有助于查明问题?
您在 let 语句中调用 FactoryGirl.build
而不是 FactoryGirl.create
。这意味着对象只存在于内存中,但实际上并不持久存在于数据库中,因此当您调用 Entity.first
时,它们在数据库中没有实体。
我在测试中更改了路线
visit 'importers#import_property_list'
至
visit import_property_list_importers_path
编辑:我最初的假设是不正确的。似乎我的测试在它完成 运行 所有代码块之前就已经达到了它的断言,但真正发生的是,我是 运行 我的完整测试套件并且我正在从一个击中相同控制器动作的不同测试。我 运行 下面的个人测试发现我什至没有击中控制器。我不得不在测试中更正我的路线。一切就绪。
原始 post 的其余部分:
NOTE: to make things easier to read, this is NOT the complete code
我的测试 'activerecord-import' gem
./spec/features/importer/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 记录
首先,有些东西看起来有点格格不入:这不应该在 before(:each) 块中吗?我通常不会像那样公开设置代码。
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
编辑:- 真正的问题
您确定 link 实际上是通过这种语法访问的:访问 'importers#import_property_list' ---> 尝试使用命名路径或适当的 url 代替。我不确定您在访问网页时是否可以使用该语法,我认为这可能是您的问题。例如试试 import_property_etc_etc_list_named_path –
这应该有助于查明问题?
您在 let 语句中调用 FactoryGirl.build
而不是 FactoryGirl.create
。这意味着对象只存在于内存中,但实际上并不持久存在于数据库中,因此当您调用 Entity.first
时,它们在数据库中没有实体。
我在测试中更改了路线
visit 'importers#import_property_list'
至
visit import_property_list_importers_path