What is preferred method to test Angular/Rails app? - javascript

I have some Angular/Rails app where Rails part is just JSON API, all front works is doing by AngularJS. I've made model/controller test already using RSpec, and now I want to make some integration tests. Earlier I use request test (with Capybara, Selenium) of RSpec to test my pages/JS, but Angular is often tested by Karma/Jasmine, and I don't understand what way is preferred in Angular/Rails app. Please, give me advice, may be you share some experience with me. Thanks.

First of all you should test everything wisely. I will recommend great series of blog posts from the link below.
Since you are already familiar with RSpec, keep using it for your rails tests like models, controllers, request, etc.
For your Angularjs tests, karma and Jasmine are great choices. But Rails uses Sprockets for asset management so configuring karma can be a little bit tricky. For Rails and Jasmine I wrote a rake task. To run javascript tests you should simply run rake karma:start. I shared the link of the gist below.
The gist contains 2 files. You should put them:
karma.rake -> lib/tasks
karma.unit.js -> spec/javascripts/
Finally you should run feature/acceptance tests with Capybara.
Testing Series Intro
The Gist of Rake Task and Karma Configuration

Related

How do I watch tested files with MochaJS?

I am testing the javascript that's sitting on top of my Rails app (using sprockets) with NPM (which is not connected to my Rails app in any way).
Everything is working great - the tests run (using NPM).
But how do I tell MochaJS which JavaScript files I want to test in my Rails app?
After doing some research I found out one would need to use a task runner such as Gulp, Grunt, or Webpack along with MochaJS to watch the files being tested.
You may want to look into konacha gem which integrates mocha and chai in the rails workflow.
Konacha ... is a Rails engine that allows you to test your JavaScript with the Mocha test framework and chai assertion library.
It ... does not attempt to be framework agnostic. By sticking with Rails, Konacha can take full advantage of features such as the asset pipeline and engines.
It can be integrated with guard through guard-konacha to watch for files and re-run tests.

Integration testing with Karma

Until now, I've done all my testing with just Jasmine and jQuery to automate the browser. I am starting to explore Karma but it seems to be set up only for unit tests that involve loading HTML fragments as test fixtures and I find no way to load a whole web page. Am I missing something or is this simply the wrong tool for what I am trying to do?
From the karma FAQ's section:
Can I use Karma to do end to end testing?
Karma has primarily been designed for low level (unit) testing. If it's an AngularJS app, you can use Karma with the karma-ng-scenario plugin. However, we recommend Protractor for high-level testing.
Typically, loading a whole web page goes beyond the scope of single units of code: you need to spin up a browser, load the page and make your actions and assertions. End-to-end testing frameworks like Protractor, Nightwatch.js or Webdriver.io might be more suitable for this specific case.
You can do integration testing using the following toolchain:
Karma ➔ Mocha ➔ Mocha-CasperJS ➔ CasperJS.
Check out the mocha-casperjs package.

How to set up an automated testing environment for AngularJS

I have spent hours trying to set up an automated testing environment for my AngularJS applications that I can run from Maven (capturing results in Bamboo)
A Google search reveals frameworks galore, based on Jasmine, Karma and generic JavaScript frameworks.
But for some reason the installation of these never goes as described.
Can anyone just point me to a set of downloads that I can install manually to proper directories, that will just execute my unit tests?
I asked a similar question previously and got down checked and requests to close. If you need to check me down, please leave a reason, as I am besides myself with frustration trying to solve this, and I am sure there many other developers experiencing the same issue
Testing javascript is not a totally mature thing, so there isn't a great diversity of good tools for doing so. Jasmine and Karma are the current best ways of doing that.
Those tools are best managed using bower, which is a nodejs package. So you'll have to install, in this order:
nodejs
npm (node package manager)
bower (using npm)
jasmine and karma using bower
phantomjs, and whatever other dependencies your javascript needs (presumably angular)
Then you can run the tests.
I found this package for maven with some brief googling, but god knows how to set it up:
http://searls.github.io/jasmine-maven-plugin/
There are many tutorials on going through this process. You have to accept that these are currently the best tools for doing this and give it your best shot. If you post more specific issues regarding the 'installation that never goes as described' of jasmine and karma you will get assistance.
you can use for example yeoman or angular-seed. both will require nodejs and optionally ruby. both will set you with phantomjs, jasmine, karma, bower and many more. however i would avoid combining that build with maven. imho it's far from perfect. java tools will never be as good as native js tools for building js project. if you really want to combine the builds then use java only to call nodejs and nothing more

Backbone integration tests: Zombie.js and RequireJS, how to?

I'm trying to test my Backbone.js web application.
Since this application is basically only consuming web services, there is not really business logic inside, and the app is simply addressing calls to our API.
I'd like to use Zombie.js for ma integration test suit, which works - for the moment - with Karma and Jasmine.
My problem is I'm using RequireJS, and because Zombie doesn't seem to be AMD compliant, I can't import Zombie into my test view.
Any ideas?
Can you include zombie js in your test runner? I think karma has a files array in its config
source: http://toon.io/how-to-setup-karma-javascript-test-runner/

Grunt + Rails just for test javascript

I've seen many posts in stackoverflow about Grunt + Rails, but many cases asked about changes from Grunt.js to Rails asset pipeline, that is not my case.
My Gruntfile was set up to work with Mocha, in other words, just for test javascript codes.
So my doubt is: How the best way to organize my files into rails project?
I thought about creating a directory in my test path called "javascript" and put all test into there, after change options in Gruntfile.
Is this the best way?
We had some Jasmine specs in our recent rails project. You could follow Jasmine's convention:
project root/
spec/
javascript/
Since another JavaScript testing framework has already done what you are suggesting, creating a "javascript" folder in your "test" folder would work just fine.

Categories

Resources