Angular 使用 Poltergeist/Capybara 进行测试时控制器未加载
Angular controller does not load when testing with Poltergeist/Capybara
我是第一次使用 rails/angular,我现在正在学习使用 poltergeist/capybara 测试 angular。 angular 在实际浏览器中一切正常,但在测试时出现 DOA。我曾尝试使用 gem 'capybara-angular',但我尝试过的 3 或 4 个配置中的 none 都做了任何事情(更不用说任何有用的事情了)。我尝试抑制 JS 错误,这实际上让 poltergeist 进入实际测试但失败了,当然,因为 angular 未初始化。
抱歉,如果这是一个愚蠢的问题,但经过 2 小时的搜索,我什至找不到其他遇到此错误的人。
错误:
Failure/Error: click_link "Search"
Capybara::Poltergeist::JavascriptError:
One or more errors were raised in the Javascript code on the page. If you don't care about these errors, you can ignore them by setting js_errors: false in your Poltergeist configuration (see documentation for details).
Error: [ng:areq] Argument 'mainCtrl' is not a function, got undefined
rails_helper.rb:
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'spec_helper'
require 'rspec/rails'
require 'capybara/poltergeist'
Capybara.javascript_driver = :poltergeist
Capybara.default_driver = :poltergeist
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = false
config.infer_spec_type_from_file_location!
config.filter_rails_from_backtrace!
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
config.before(:each, :type => :feature) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
angular_test_spec.rb:
require 'rails_helper'
feature "angular test" do
let(:email){ "test@user.com" }
let(:password){ "password123" }
before do
User.create!(email: email,
password: password,
password_confirmation: password)
end
scenario "Angular App is working" do
visit "/"
#login
fill_in "Email", with: "test@user.com"
fill_in "Password", with: "password123"
click_button "Log in"
#Check that navigation worked
expect(page).to have_content("Home")
#Test the nav
click_link "Search"
#Check that navigation worked
expect(page).to have_content("Search")
fill_in "keywords", with: "some item"
within "section div h3" do
expect(page).to have_content("some item")
end
end
end
编辑:
Angular 绑定在这样的测试页上工作:
<input name="test" ng-model="test">
<p>{{ test }}</p>
但是,控制器未加载。
编辑 2:
我想既然一切都在开发中工作但没有测试资产管道中发生了一些不好的事情并开始谷歌搜索。我偶然发现了这个博客:http://www.gbonfant.com/blog/debugging-javascript-capybara/(水豚测试中更好的 JavaScript 错误)
显然,更好的错误描述听起来很棒,所以我将 config.assets.debug = true
添加到 /config/environments/test.rb
,现在测试顺利通过,没有任何错误。我怀疑根据 Tom Walpole 的评论,这是可行的,因为 Rails 正在提供非串联资产。
确保您使用的是 PhantomJS 2.1+ - PhantomJS < 2.0 不支持 angular
所需的 Function.prototype.bind()
我是第一次使用 rails/angular,我现在正在学习使用 poltergeist/capybara 测试 angular。 angular 在实际浏览器中一切正常,但在测试时出现 DOA。我曾尝试使用 gem 'capybara-angular',但我尝试过的 3 或 4 个配置中的 none 都做了任何事情(更不用说任何有用的事情了)。我尝试抑制 JS 错误,这实际上让 poltergeist 进入实际测试但失败了,当然,因为 angular 未初始化。
抱歉,如果这是一个愚蠢的问题,但经过 2 小时的搜索,我什至找不到其他遇到此错误的人。
错误: Failure/Error: click_link "Search"
Capybara::Poltergeist::JavascriptError:
One or more errors were raised in the Javascript code on the page. If you don't care about these errors, you can ignore them by setting js_errors: false in your Poltergeist configuration (see documentation for details).
Error: [ng:areq] Argument 'mainCtrl' is not a function, got undefined
rails_helper.rb:
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'spec_helper'
require 'rspec/rails'
require 'capybara/poltergeist'
Capybara.javascript_driver = :poltergeist
Capybara.default_driver = :poltergeist
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = false
config.infer_spec_type_from_file_location!
config.filter_rails_from_backtrace!
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
config.before(:each, :type => :feature) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
angular_test_spec.rb:
require 'rails_helper'
feature "angular test" do
let(:email){ "test@user.com" }
let(:password){ "password123" }
before do
User.create!(email: email,
password: password,
password_confirmation: password)
end
scenario "Angular App is working" do
visit "/"
#login
fill_in "Email", with: "test@user.com"
fill_in "Password", with: "password123"
click_button "Log in"
#Check that navigation worked
expect(page).to have_content("Home")
#Test the nav
click_link "Search"
#Check that navigation worked
expect(page).to have_content("Search")
fill_in "keywords", with: "some item"
within "section div h3" do
expect(page).to have_content("some item")
end
end
end
编辑:
Angular 绑定在这样的测试页上工作:
<input name="test" ng-model="test">
<p>{{ test }}</p>
但是,控制器未加载。
编辑 2:
我想既然一切都在开发中工作但没有测试资产管道中发生了一些不好的事情并开始谷歌搜索。我偶然发现了这个博客:http://www.gbonfant.com/blog/debugging-javascript-capybara/(水豚测试中更好的 JavaScript 错误)
显然,更好的错误描述听起来很棒,所以我将 config.assets.debug = true
添加到 /config/environments/test.rb
,现在测试顺利通过,没有任何错误。我怀疑根据 Tom Walpole 的评论,这是可行的,因为 Rails 正在提供非串联资产。
确保您使用的是 PhantomJS 2.1+ - PhantomJS < 2.0 不支持 angular
所需的 Function.prototype.bind()