I have an account and user factory. Now I want to test the account Factory.
In the test I want to check whether new User() was called and fake data is returned.
angular.module('app')
.factory 'Account', [ 'User', (User) ->
class @Account
constructor: (account) ->
@self = this
@user = new User(account.user_id)
]
.factory 'User', [ '$http', ($http) ->
class @User
constructor: (id)
$http.get('/user.json?id='+id)
.success (data) =>
//do something
.error (data) ->
//do something
]
Test:
describe 'app', ->
describe 'Account', ->
Account = undefined
User = undefined
beforeEach(module('app'))
beforeEach inject((_Account_, _User_) ->
Account = _Account_
User = _User_
)
describe 'initialize', ->
it 'should call new User', ->
spyOn(window, 'User').and.callFake( (value) ->
return value
)
account = new Account({ id: 1 })
I got always: Error: User() method does not exist
Here a fiddle
When I remove the spy the test is green and the console.log is called. When I add the spyOn, I got the error.
How do I make a test to check if the new User is called?