spyon

Jasmine spyOn with specific arguments

耗尽温柔 提交于 2020-07-17 09:28:13
问题 Suppose I have spyOn($cookieStore,'get').and.returnValue('abc'); This is too general for my use case. Anytime we call $cookieStore.get('someValue') --> returns 'abc' $cookieStore.get('anotherValue') --> returns 'abc' I want to setup a spyOn so I get different returns based on the argument: $cookieStore.get('someValue') --> returns 'someabc' $cookieStore.get('anotherValue') --> returns 'anotherabc' Any suggestions? 回答1: You can use callFake: spyOn($cookieStore,'get').and.callFake(function(arg)

Jasmine.js Testing - spy on window.navigator.userAgent

落爺英雄遲暮 提交于 2020-01-03 16:38:11
问题 I need to find the way to change userAgent value. I tried to spyOn the window.navigator.userAgent . But that's not helping. JS : @Injectable() export class DetectBrowserService { browserIE: boolean; constructor() { this.browserIE = this.detectExplorer(); } public detectExplorer() { const brows = window.navigator.userAgent; const msie = brows.indexOf('MSIE '); if (msie > 0) { // IE 10 or older => return version number return true; } } } Spec : it('should test window.navigator.userAgent', () =>

Jasmine.js Testing - spy on window.navigator.userAgent

末鹿安然 提交于 2020-01-03 16:37:26
问题 I need to find the way to change userAgent value. I tried to spyOn the window.navigator.userAgent . But that's not helping. JS : @Injectable() export class DetectBrowserService { browserIE: boolean; constructor() { this.browserIE = this.detectExplorer(); } public detectExplorer() { const brows = window.navigator.userAgent; const msie = brows.indexOf('MSIE '); if (msie > 0) { // IE 10 or older => return version number return true; } } } Spec : it('should test window.navigator.userAgent', () =>

Unit testing with private service injected using jasmine angular2

巧了我就是萌 提交于 2020-01-02 02:21:26
问题 I have a problem trying to unit test an angular service. I want to verify that this service is properly calling another service that is injected into it. Lets say I have this ServiceToTest that injects ServiceInjected: ServiceToTest .service.ts @Injectable() export class ServiceToTest { constructor(private _si: ServiceInjected) {} public init() { this._si.configure(); } } ServiceInjected.service.ts @Injectable() export class ServiceInjected { constructor() {} public configure() { /*Some

Jest unit test to spy on lower-level method (NodeJS)

醉酒当歌 提交于 2019-12-02 14:13:34
问题 Trying to spy and override a function two levels down using Jest. The test results say, "Expected mock function to have been called, but it was not called." // mail/index.unit.test.js import mail from './index'; import * as sib from '../sendinblue'; describe('EMAIL Util', () => test('should call sibSubmit in server/utils/sendinblue/index.js', async() => { const sibMock = jest.spyOn(sib, 'sibSubmit'); sibMock.mockImplementation(() => 'Calling sibSubmit()'); const testMessage = { sender: [{

Jest unit test to spy on lower-level method (NodeJS)

筅森魡賤 提交于 2019-12-02 06:54:47
Trying to spy and override a function two levels down using Jest. The test results say, "Expected mock function to have been called, but it was not called." // mail/index.unit.test.js import mail from './index'; import * as sib from '../sendinblue'; describe('EMAIL Util', () => test('should call sibSubmit in server/utils/sendinblue/index.js', async() => { const sibMock = jest.spyOn(sib, 'sibSubmit'); sibMock.mockImplementation(() => 'Calling sibSubmit()'); const testMessage = { sender: [{ email: 'foo@example.com', name: 'Something' }], to: [{ email: 'foo@example.com', name: 'Something' }],