如何存根的茉莉模拟对象的方法?

根据 Jasmine 文档,模拟可以这样创建:

jasmine.createSpyObj(someObject, ['method1', 'method2', ... ]);

如何破坏其中一种方法?例如,如果您想测试当一个方法抛出异常时会发生什么,您将如何做呢?

106605 次浏览

You have to chain method1, method2 as EricG commented, but not with andCallThrough() (or and.callThrough() in version 2.0). It will delegate to real implementation.

In this case you need to chain with and.callFake() and pass the function you want to be called (can throw exception or whatever you want):

var someObject = jasmine.createSpyObj('someObject', [ 'method1', 'method2' ]);
someObject.method1.and.callFake(function() {
throw 'an-exception';
});

And then you can verify:

expect(yourFncCallingMethod1).toThrow('an-exception');

If you are using Typescript, it's helpful to cast the method as Jasmine.Spy. In the above Answer (oddly I don't have rep for comment):

(someObject.method1 as Jasmine.Spy).and.callFake(function() {
throw 'an-exception';
});

I don't know if I'm over-engineering, because I lack the knowledge...

For Typescript, I want:

  • Intellisense from the underlying type
  • The ability to mock just the methods used in a function

I've found this useful:

namespace Services {
class LogService {
info(message: string, ...optionalParams: any[]) {
if (optionalParams && optionalParams.length > 0) {
console.log(message, optionalParams);
return;
}


console.log(message);
}
}
}


class ExampleSystemUnderTest {
constructor(private log: Services.LogService) {
}


doIt() {
this.log.info('done');
}
}


// I export this in a common test file
// with other utils that all tests import
const asSpy = f => <jasmine.Spy>f;


describe('SomeTest', () => {
let log: Services.LogService;
let sut: ExampleSystemUnderTest;


// ARRANGE
beforeEach(() => {
log = jasmine.createSpyObj('log', ['info', 'error']);
sut = new ExampleSystemUnderTest(log);
});


it('should do', () => {
// ACT
sut.doIt();


// ASSERT
expect(asSpy(log.error)).not.toHaveBeenCalled();
expect(asSpy(log.info)).toHaveBeenCalledTimes(1);
expect(asSpy(log.info).calls.allArgs()).toEqual([
['done']
]);
});
});

Building on @Eric Swanson's answer, I've created a better readable and documented function for using in my tests. I also added some type safety by typing the parameter as a function.

I would recommend to place this code somewhere in a common test class, so that you can import it in every test file that needs it.

/**
* Transforms the given method into a jasmine spy so that jasmine functions
* can be called on this method without Typescript throwing an error
*
* @example
* `asSpy(translator.getDefaultLang).and.returnValue(null);`
* is equal to
* `(translator.getDefaultLang as jasmine.Spy).and.returnValue(null);`
*
* This function will be mostly used in combination with `jasmine.createSpyObj`, when you want
* to add custom behavior to a by jasmine created method
* @example
* `const translator: TranslateService = jasmine.createSpyObj('TranslateService', ['getDefaultLang'])
* asSpy(translator.getDefaultLang).and.returnValue(null);`
*
* @param {() => any} method - The method that should be types as a jasmine Spy
* @returns {jasmine.Spy} - The newly typed method
*/
export function asSpy(method: () => any): jasmine.Spy {
return method as jasmine.Spy;
}

Usage would be as follows:

import {asSpy} from "location/to/the/method";


const translator: TranslateService = jasmine.createSpyObj('TranslateService', ['getDefaultLang']);
asSpy(translator.getDefaultLang).and.returnValue(null);

Angular 9

Using jasmine.createSpyObj is ideal when testing a component where a simple service is injected. For example: let's say, in my HomeComponent I have a HomeService (injected). The only method in the HomeService is getAddress(). When creating the HomeComponent test suite, I can initialize the component and service as:

describe('Home Component', () => {
let component: HomeComponent;
let fixture: ComponentFixture<HomeComponent>;
let element: DebugElement;
let homeServiceSpy: any;
let homeService: any;


beforeEach(async(() => {
homeServiceSpy = jasmine.createSpyObj('HomeService', ['getAddress']);


TestBed.configureTestingModule({
declarations: [HomeComponent],
providers: [{ provide: HomeService, useValue: homeServiceSpy }]
})
.compileComponents()
.then(() => {
fixture = TestBed.createComponent(HomeComponent);
component = fixture.componentInstance;
element = fixture.debugElement;
homeService = TestBed.get(HomeService);
fixture.detectChanges();
});
}));


it('should be created', () => {
expect(component).toBeTruthy();
});


it("should display home address", () => {
homeService.getAddress.and.returnValue(of('1221 Hub Street'));
fixture.detectChanges();


const address = element.queryAll(By.css(".address"));


expect(address[0].nativeNode.innerText).toEqual('1221 Hub Street');
});
});

This is a simple way to test your component using jasmine.createSpyObj. However, if your service has more methods more complex logic, I would recommend creating a mockService instead of createSpyObj. For example: providers: [{ provide: HomeService, useValue: MockHomeService }]

Hope this helps!