Issue: Angular controller fails to load during testing with Poltergeist/CapybaraExplanation: While conducting

Exploring the world of rails/angular for the first time has been an interesting journey. Currently, I am focusing on testing angular with poltergeist/capybara. While everything runs smoothly in the actual browser, it appears lifeless during testing. Despite attempts to use the gem 'capybara-angular' and experimenting with different configurations, nothing seemed to work in my favor. Suppressing JS errors did allow poltergeist to progress with the test, but alas, it failed because angular was not initialized.

I apologize if this question seems trivial, as I have scoured the internet for a solution for over 2 hours without success.

The encountered error: Failure/Error: click_link "Search"

 Capybara::Poltergeist::JavascriptError:
   Errors were raised in the Javascript code on the page. If you wish to ignore these errors, set js_errors: false in your Poltergeist configuration.

   Error: [ng:areq] Argument 'mainCtrl' is 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){ "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4e3a2b3d3a0e3b3d2b3c602d2123">[email protected]</a>" }
  let(:password){ "password123" }

 before do
   User.create!(email: email,
               password: password,
               password_confirmation: password)
end

 scenario "Angular App is working" do
   visit "/"

   fill_in "Email", with: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="295d4c5a5d695c5a4c5b074a4644">[email protected]</a>"
   fill_in "Password", with: "password123"
   click_button "Log in"

   expect(page).to have_content("Home")

   click_link "Search"

   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

EDIT:

While Angular bindings function on a test page such as this:

<input name="test" ng-model="test">
<p>{{ test }}</p>

Unfortunately, controllers fail to initialize.

EDIT 2:

Drawing from the fact that things run smoothly in development but not during testing, I suspect an issue lies within the asset pipeline. Research led me to this insightful blog: (Better JavaScript errors in Capybara tests)

The promise of clearer error descriptions intrigued me, so I added config.assets.debug = true to /config/environments/test.rb. Miraculously, the tests now pass without any errors. This success could be attributed to Rails serving non-concatenated assets, as per Tom Walpole's insights.

Answer №1

Ensure that your PhantomJS version is 2.1 or higher - Previous versions of PhantomJS did not support Function.prototype.bind() which is necessary for angular

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Implementing a fixed value instead of a variable in the HTML for a directive attribute

I am looking to implement a directive that requires an attribute. I want this attribute to always be set to true without the need for a scope variable. However, I suspect that using "true" directly as the attribute value may be incorrect, as it might be in ...

using javascript to animate multiple div elements

In my current project, I am harnessing the power of Javascript to incorporate some eye-catching animation effects on a rectangle. Once the animation is complete, I have set the box to disappear from view. Check out the code snippet below for more details: ...

Transform a JSON object string into a usable value

I'm currently working on a function that takes a string, splits it, and then formats it using json[key][key2][key3]. The issue is that 'n' could potentially be infinite (not literally but needs to be written as such) function getJsonValue(j ...

"Triggering an AJAX POST request with a click event, unexpectedly receiving a GET response for

Hello there! I am currently attempting to send an AJAX POST request when a button is clicked. Below is the form and button that I am referring to: <form class="form-horizontal" > <fieldset> <!-- Form Name --> <legend> ...

Exploring the power of TypeScript within the useContext hook

I recently started following a React tutorial on YouTube and decided to convert the project from JavaScript to TypeScript. However, I am facing difficulties with implementing useContext in my code. Any help or guidance would be greatly appreciated. If yo ...

Error: There was an unexpected token found in the $http.get() request syntax

When attempting to create my first Angular app, I encountered the following error Here is the JavaScript code snippet: var app = angular.module('sortApp', []) app.controller('sortController', ['$http', function ($http) { ...

The modal window occasionally malfunctions in various browsers

After developing an application using Ruby on Rails, I decided to add a modal window for the login feature. I included the modal code in the partial layouts/header section, expecting it to work seamlessly on every page of my app. However, I encountered an ...

Tips for transferring v-model data between components

I am working with a parent form component and a child component, both located in separate files. I am using the Quasar Framework components. How can I pass data from the parent to the child component using v-model? Parent Component <template> < ...

What is the best way to avoid using ng-repeat for the last item being shown?

I currently have an ng-repeat loop that is dependent on an ng-if condition. Here is the specific code block: <div class="fav-prod-customised> <p ng-repeat="ingredient in product.options" ng-if="ingredient.default_quantity == 0 & ...

Retrieve data from MongoDB using the find() method results in an empty response, however,

While working on a project to practice my MongoDB skills, I encountered an issue with retrieving all the data from MongoDB. Despite receiving a successful 200 response, I was unable to properly extract all the data. Using Express framework for this task, ...

Including v-menu in a button causes it to vanish in Vuetify

I am facing an issue with my stepper. Specifically, in step 3, I am trying to add a v-menu to a button but when I do so, the button disappears. Below is the code snippet causing the problem: <template> . . . . <v-stepper-step :complete="e6 > ...

Steps for loading a different local JavaScript file by clicking on a button

My goal is to reload the browser page and display a different ReactJS component/file when a button is pressed. Initially, I attempted using href="./code.js" or window.location="./code.js" within the button props, but unfortunately, it did not yield the des ...

Having trouble with Instafeed JS loading?

I am currently experimenting with instafeed.js, but I am encountering difficulties getting it to load on a basic bootstrap page. While everything else on my page loads successfully, this particular container remains empty without any Instagram code presen ...

After signing out and logging back in, data fails to display - ReactJS

I am encountering difficulties with the login/logout process and displaying data in a form. When I restart my server and log in without logging out, everything works fine and the data appears in the form. However, if I log in, then log out, and log back in ...

Tips for implementing react portal functionality in react 15

Working on my current project with react 15, I am looking to display an overlay on the screen when a dropdown is opened. While React 16 allows us to utilize React portal to render children into a DOM node outside of the parent component's hierarchy, h ...

The Twilio API is failing to deliver the response in JSON format

I have been working on an API to send WhatsApp responses to clients using Twilio as the service provider. However, I am encountering a problem where I am unable to receive the response in JSON format, despite following the code examples provided by Twilio. ...

Ways to select elements from an array of objects based on date

I'm currently working on a task that involves filtering an array of objects to find the ones closest to today. Unfortunately, I encountered an issue where the code is returning dates in June instead of May. Here's the snippet of code I've be ...

What is the procedure for initiating a POST request when the payload is ready for submission?

After a successful payment, my success page is displayed with a URL that includes a query parameter. I want to make a POST request to my API endpoint when the page loads, but I'm encountering an issue where the first POST request is empty (router.quer ...

What could be causing the Toast message to not show up in react-native-root-toast?

Incorporated react-native-root-toast into my expo project running on expo 51. Please see the code snippet below for reference: const toastColors = { 'error': { color: '#DA5C53', iconName: <WarningIcon size="5 ...

How to disable click event binding in Angular2 after it has been clicked once

Within my application, there is a button that has a click event attached to it: <button class="btn btn-default" (click)="doSomething()"> I am wondering if there is a way to remove the (click) event from the button within the doSomething method so t ...