簡體   English   中英

在運行代碼之前,Rails 4 rspec測試失敗

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

編輯:我最初的假設是不正確的。 似乎我的測試在完成所有代碼塊之前就已經達到了斷言,但是真正發生的是,我正在運行完整的測試套件,並且從另一個測試中獲得了輸出,該測試正在擊中同一個控制器行動。 我在下面運行了單個測試,發現我什至沒有碰到控制器。 我必須在測試中更正我的路線。 全部設置好了。

其余原始帖子:

注意:為了使內容更易於閱讀,這不是完整的代碼

我對“ 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是否為nil? 並檢查該塊是否實際上在循環遍歷:

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

編輯:-真正的問題

您確定鏈接實際上是通過這種語法訪問的:訪問'importers#import_property_list'--->嘗試使用命名路徑或正確的URL。 我不確定您在訪問網頁時是否可以使用該語法,我認為這可能是您的問題。 例如,嘗試import_property_etc_etc_list_named_pa​​th –

那應該有助於找出問題所在?

您在let語句中調用FactoryGirl.build而不是FactoryGirl.create 這意味着對象僅存在於內存中,而實際上並不持久存在於數據庫中,因此,當您調用Entity.first它們在數據庫中不是實體。

我從更改了測試的路線

visit 'importers#import_property_list'

visit import_property_list_importers_path

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM