Reputation: 13
I am working with rails/angular for the first time and I'm just now learning to test angular with poltergeist/capybara. Everything angular is working fine in the actual browser but appears DOA when in testing. I have tried using the gem 'capybara-angular' but none of the 3 or 4 configurations I tried did anything (let alone anything helpful). I tried suppressing the JS errors which actually lets poltergeist get to the actual test but that fails, of course, because angular is not initialized.
Sorry if this is a dumb question, but I haven't even been able to find anybody else experiencing this error after 2 hours of searching.
The error: 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){ "[email protected]" }
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: "[email protected]"
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 bindings are working on a test page like this:
<input name="test" ng-model="test">
<p>{{ test }}</p>
However, controllers do not load.
I figured since things are working in dev but not test that something bad is happening in the asset pipeline and started googling. I stumbled on this blog: http://www.gbonfant.com/blog/debugging-javascript-capybara/ (Better JavaScript errors in Capybara tests)
Better error descriptions sounded great, obviously, so I added config.assets.debug = true
to /config/environments/test.rb
and now the tests are passing without any errors. I suspect per Tom Walpole's comment that this works because Rails is serving non-concatenated assets.
Upvotes: 1
Views: 567
Reputation: 49870
Make sure you are using PhantomJS 2.1+ - PhantomJS < 2.0 did not have support for Function.prototype.bind() which is required by angular
Upvotes: 2