Search code examples
ruby-on-railsruby-on-rails-4rspecrspec-rails

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

./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

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

enter image description here

NOTE: the first test only passes because of my factory_girl's factory association which creates CashAccount records


Solution

  • Firstly, something that looked a little out of place: shouldn't this be in the before(:each) block? 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| 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? 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. I'm not sure you can use that syntax when visiting a webpage and that's probably your problem i think. e.g. try import_property_etc_etc_list_named_path –

    that should help pinpoint the problem?