简体   繁体   English

在运行代码之前,Rails 4 rspec测试失败

[英]Rails 4 rspec test fails before code is run

EDIT: My initial assumption was incorrect. 编辑:我最初的假设是不正确的。 It seemed like my test was hitting its assertions before it even finished running all of its code blocks, but what was really happening was, I was running my full test suite and I was getting the output from a different test that was hitting the same controller action. 似乎我的测试在完成所有代码块之前就已经达到了断言,但是真正发生的是,我正在运行完整的测试套件,并且从另一个测试中获得了输出,该测试正在击中同一个控制器行动。 I ran the individual test below and found that I wasn't even hitting the controller. 我在下面运行了单个测试,发现我什至没有碰到控制器。 I had to correct my route in the test. 我必须在测试中更正我的路线。 All set now. 全部设置好了。

Rest of original post: 其余原始帖子:

NOTE: to make things easier to read, this is NOT the complete code 注意:为了使内容更易于阅读,这不是完整的代码

My test with 'activerecord-import' gem 我对“ activerecord-import” gem的测试

./spec/features/importer/importer_property_list_spec.rb ./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

My controller ./app/controllers/importers_controller.rb 我的控制器./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

This is my output in my console 这是我在控制台中的输出

在此处输入图片说明

NOTE: the first test only passes because of my factory_girl's factory association which creates CashAccount records 注意:第一次测试仅通过,因为我的factory_girl的工厂关联会创建CashAccount记录

Firstly, something that looked a little out of place: shouldn't this be in the before(:each) block? 首先,看起来有些不合时宜的东西:这不应该在before(:each)块中吗? i normally don't leave set up code in the open like that. 我通常不会像那样公开设置代码。

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) }

Basically this is saying that there are no Entity records created. 基本上,这就是说没有创建实体记录。 Where are you creating the entity records? 您在哪里创建实体记录? in the block here: (4..excel.last_row).each do |row| 在此处的代码块中: (4..excel.last_row).each do |row| but is the block even running? 但是该块是否还在运行? Look to see whether there are any legitimate values in excel = property_list_excel - please check that excel is not nil? 看看excel = property_list_excel是否有合法值-请检查excel是否为nil? and also check whether the block is actually looping through: 并检查该块是否实际上在循环遍历:

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

edit: - the real problem 编辑:-真正的问题

are you sure the link is actually visiting via this type of syntax: visit 'importers#import_property_list' ---> try using the named path or a proper url instead. 您确定链接实际上是通过这种语法访问的:访问'importers#import_property_list'--->尝试使用命名路径或正确的URL。 I'm not sure you can use that syntax when visiting a webpage and that's probably your problem i think. 我不确定您在访问网页时是否可以使用该语法,我认为这可能是您的问题。 eg try import_property_etc_etc_list_named_path – 例如,尝试import_property_etc_etc_list_named_pa​​th –

that should help pinpoint the problem? 那应该有助于找出问题所在?

You are calling FactoryGirl.build instead of FactoryGirl.create in your let statements. 您在let语句中调用FactoryGirl.build而不是FactoryGirl.create This means that the objects only exist in memory but don't actually persist in the database, so when you call Entity.first their are no entities in the db. 这意味着对象仅存在于内存中,而实际上并不持久存在于数据库中,因此,当您调用Entity.first它们在数据库中不是实体。

I changed the route in my test from 我从更改了测试的路线

visit 'importers#import_property_list'

to

visit import_property_list_importers_path

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM