简体   繁体   English

使用Jasmine测试方法的可观察到的返回值

[英]Test observable return value of method with Jasmine

I am new to Jasmine and I am attempting to write a simple unit test that checks if my method returns the intended value. 我是Jasmine的新手,正在尝试编写一个简单的单元测试,以检查我的方法是否返回预期值。 Here is the method in my Angular application: 这是我的Angular应用程序中的方法:

saveEvent(techEvent): Observable<IEvent>{

        let headers = new Headers({ 'Content-Type': 'application/json'})
        let options = new RequestOptions({headers: headers})

        return this.http.post('/api/events', techEvent, options)
            .map((response: Response) => {
                //have an updated copy of saved event
                return response.json()
            }).catch(this.handleError)  
}

As you can see, it is a post method that saves a 'techEvent' object. 如您所见,这是一个保存“ techEvent”对象的post方法。 Here is the test I have attempted to write in my spec: 这是我尝试在规格中编写的测试:

   it('should save the event', () => {

        var testEvent = { id: 7, name: "AngularJS"}

        mockHttp.post.and.returnValue(Observable.of(false))

        techEventService.saveEvent(<IEvent>testEvent)

        expect(techEventService.saveEvent).toBe(jasmine.any(Object))

    })

This test fails :( . My goal here is to simply test that the method returns an object and returns the specific object that is passed. I wonder if I could also test if it is a JSON... 此测试失败:(。我的目标是简单地测试该方法返回一个对象并返回所传递的特定对象。我想知道是否还可以测试它是否为JSON ...

Your method is asynchronous, which is generally the case for Observables. 您的方法是异步的,通常对于Observables都是这样。 You'll want to subscribe to the response from techEventService.saveEvent and verify that the variable was changed within that subscribe callback. 您将要订阅来自techEventService.saveEvent的响应,并验证该变量在该订阅回调中是否已更改。

Additionally, you're testing whether techEventService.saveEvent is an object, but that is most definitely a reference to the techEventService.saveEvent function. 此外,您正在测试techEventService.saveEvent是否是一个对象,但这绝对是对techEventService.saveEvent函数的引用。 I believe you wanted to test if the response from the server was an object, which is another reason to run your test within the subscribe() callback. 我相信您想测试服务器的响应是否是一个对象,这是在subscribe()回调中运行测试的另一个原因。

it('should save the event', (done) => {

    var testEvent = { id: 7, name: "AngularJS"};

    mockHttp.post.and.returnValue(Observable.of(false));

    techEventService.saveEvent(<IEvent>testEvent).subscribe((response) => {
       expect(response).toBe(jasmine.any(Object));
       done();
    });
});

Because this is async, jasmine might think the test is done before your callback is actually loaded (never even reaching the expect call, and declaring your test a pass without having run it.) For that reason, you can pass in done into your test, and then jasmine won't consider your test to have completed until you call the done() callback. 因为这是异步的,所以茉莉花可能认为测试是在实际加载回调之前完成的(甚至不会到达expect调用,并且在不运行测试的情况下声明测试通过。)因此,您可以将done传递给测试,然后茉莉花将不会认为您的测试已经完成,直到您调用done()回调。

Side note: Make a habit of ending lines with semicolons when appropriate. 旁注:习惯在适当时使用分号结束行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM