Skip to content

Instantly share code, notes, and snippets.

@ignat-z
Last active August 29, 2015 14:06
Show Gist options
  • Save ignat-z/27089c17ecdeeec63856 to your computer and use it in GitHub Desktop.
Save ignat-z/27089c17ecdeeec63856 to your computer and use it in GitHub Desktop.
# Writing frontend specs
# 1. describe block
describe 'Service: Deal', ->
# 2. beforeEach block
beforeEach ->
#a.
module "dealglobeApp" # our application module
#b.
module ($translateProvider) ->
$translateProvider.translations('en', {}); # Prevent angular-translate extra queries to server
null # Make sure CoffeeScript doesn't return anything
#c.
module ($httpProvider) ->
$httpProvider.interceptors.length = 0 # Killing all interceptors (tokenInterceptor, notificationsInterceptor, authInterceptor and so on)
null # Make sure CoffeeScript doesn't return anything
# 3. afterEach block
afterEach ->
# Instead of stubbing $http angularjs provides $httpBackend that can be used to stub requests (details bellow)
httpBackend.verifyNoOutstandingExpectation(); # Verifies that all of the requests defined via the expect api were made.
httpBackend.verifyNoOutstandingRequest(); # Verifies that there are no outstanding requests that need to be flushed.
# Variables that will be available in "it" blocks, in this case only. You can define each varibale such as:
# Deal = {}
# httpBackend = {} and so on, but we can use mass assign feature of coffeescript and define it oneline
# P.S. You can define any variables, but corrently there is only angularjs defaults modules objects
[Deal, httpBackend, $ENV] = {}
# Dependency injection is main angularjs feature, so injecting dependencies in test module
beforeEach inject (_Deal_, $httpBackend, ENV) ->
Deal = _Deal_ # Notice _Deal_ instead of Deal to prevent undefined behaviour on case Deal = Deal
httpBackend = $httpBackend
$ENV = ENV
# default it behaviour
it 'should make index request', ->
httpBackend.expect('GET', "#{$ENV.endpoint}/v2/deals") # main feature of this test, we are expectiong GET request on endpoint
.respond("success": true) # and define response for this
# notice that if you will remove any of beforeEach modules, httpBackend will catch extra-queries and tests fail
Deal.index() # spec subject
httpBackend.flush(); # Flushes all pending requests
# for unit testing you can use this cheatsheet http://jasmine.github.io/2.0/introduction.html
# To start grunt testing you need to use command `grunt test` don't forget --force derective for any minor warnings
# Bad news that there is no easy way to test one spec file, but you can use watch derective to watch for file changes
# `grunt test watch` (--force)
#
# Coverage generates each test run and stores in coverage/ folder, it will contain a lot of JSON files for each test
# start and folder with name such as : "PhantomJS\ 1.9.7\ \(Mac\ OS\ X\)/" (in my case), index.html contains table
# with full coverage info
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment