EDIT:私の初期の仮定は正しくありませんでした。私のテストがすべてのコードブロックの実行を終了する前に、私のテストがそのアサーションに当たっていたように思えましたが、本当に起こったのは、私がテストスイート全体を実行していて、同じコントローラを打つ別のテストアクション。私は以下の個別のテストを実行し、私はコントローラにも当たらないことがわかった。私はテストで自分のルートを修正しなければならなかった。すべてセットされました。オリジナルのポストのコードが実行される前にRails 4 rspecテストが失敗する
レクリエーション:
注:読み、物事を容易にするために、これは 'ActiveRecordのインポート' 宝石
との完全なコード
私のテストではありません。 /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レコードを作成する
に私のテストでルートを変更し、別のテストでは、「入力/終了のインポート」を出力します。あなたは正しい軌道に乗っています。このテストファイルだけを実行すると、コントローラーのアクションにも当てはまりません。 – wsgb
@wsgbこのタイプの構文でリンクが実際に訪れていることを確信しています:visit 'importers#import_property_list' --->代わりに名前付きパスまたは適切なURLを使用してみてください。私はあなたがWebページを訪問するときにその構文を使用することができないと私は思うあなたの問題はおそらくだ。例えばtry import_property_etc_etc_list_named_path – BKSpurgeon
ありがとうございます!それがまさに問題でした。それをあなたの投稿に追加することができれば、あなたの投稿を回答としてマークします。 – wsgb