I would like to test my angular application with Jasmine.
So I created some tests, most of them work fine.
But, one of my functions require the user to fill in an prompt.
Tests can not fill this prompt, so I mocked them with spyOn(window,'prompt').and.returnValue('test')
. This works, but only once.
When I add two of my components (the function in which the prompt is located), I want to spyOn
the first prompt with result 'test', and the second prompt with 'test2'. I tried doing this as follows:
it 'should place the component as last object in the form', ->
spyOn(window, 'prompt').and.returnValue('test')
builder.addFormObject 'default', {component: 'test'}
spyOn(window, 'prompt').and.returnValue('test2')
builder.addFormObject 'default', {component: 'test2'}
expect(builder.forms['default'][0].name).toEqual('test')
But this gives the following error: Error: prompt has already been spied upon
This is quite logical, but I don't know another way of returning with a spyOn.
So, what I want is this: Before the first addFormObject I want to spy on the prompt which returns 'test'. And the second addFormObject I want to spy with return 'test2'
With spyOn you can return mocked value and set it dynamically like in following code