单元测试失败时,函数调用上启动函数、单元测试

2023-09-14 00:20:13 作者:-    败家 ° - 

这是一个扩展了我的前一个问题:$httpBackend在AngularJs茉莉花单元测试

在我的控制器中,getStuff函数被调用的启动。这引起我的单元测试失败。当我注释掉,我的单元测试通过,并成功运行。如果取消注释,错误的是:

错误:意外的请求:GET / API /东东没有更多的要求有望

我的控制器是:

  $ scope.stuff = [];$ scope.getStuff =功能(){    VAR URL = site.root +'API /东西';    $ http.get(URL)        .success(功能(数据){            $ scope.stuff =数据;        })        .error(功能(错误){            的console.log(错误);        });};// $ scope.getStuff(); 
第4章一次函数 单元测试 A卷

和我的单元测试是:

 它('应该得到的东西',函数(){    VAR URL ='/ API /东西';    变种的htt presponse = [{stuffId:1}​​,{stuffId:2}];    httpLocalBackend.expectGET(URL).respond(200,HTT presponse);    $ scope.getStuff();    httpLocalBackend.flush();    期待($ scope.stuff.length).toBe(2);}); 

一切的单元测试明智的,做工精细这样。不幸的是,这打破了实际网站的功能。当我取消控制器,单元测试断裂的最后一行,和网站的作品。任何帮助是AP preciated。谢谢!

固定:感谢fiskers7的答案,这是我的解决方案

 它('应该得到的东西',函数(){    VAR URL ='/ API /东西';    变种的htt presponse = [{stuffId:1}​​,{stuffId:2}];    httpLocalBackend.expectGET(URL).respond(200,HTT presponse);    httpLocalBackend.expectGET(URL).respond(200,HTT presponse);    $ scope.getStuff();    httpLocalBackend.flush();    期待($ scope.stuff.length).toBe(2);}); 

解决方案

逐字从我的评论:

在创建它对API /东西',当你在测试中调用$ scope.getStuff()再次调用它的呼叫控制器。因此,而不是一个调用API /东西'你有其中两个有什么错误的话。 httpBackend没想到两次调用端点,唯一一所以它抛出的错误。

从我这个答案评论code例如,如果您需要看到它。

 它('应该得到的东西',函数(){  VAR URL ='/ API /东西';  变种的htt presponse = [{stuffId:1}​​,{stuffId:2}];  httpLocalBackend.expectGET(URL).respond(200,HTT presponse);  httpLocalBackend.flush();  期待($ scope.stuff.length).toBe(2);}); 

This is an extension off a former question of mine: $httpBackend in AngularJs Jasmine unit test

In my controller, the getStuff function is called on startup. This causes my unit test to fail. When I comment it out, my unit tests pass and work successfully. When uncommented, the error is:

Error: Unexpected request: GET /api/stuff No more request expected

My controller is:

$scope.stuff = [];
$scope.getStuff = function () {
    var url = site.root + 'api/stuff';
    $http.get(url)
        .success(function (data) {
            $scope.stuff = data;
        })
        .error(function(error) {
            console.log(error);
        });
};
//$scope.getStuff();

and my unit test is:

it('should get stuff', function () {
    var url = '/api/stuff';
    var httpResponse = [{ "stuffId": 1 }, { "stuffId": 2 }];
    httpLocalBackend.expectGET(url).respond(200, httpResponse);
    $scope.getStuff();
    httpLocalBackend.flush();
    expect($scope.stuff.length).toBe(2);
} );

Everything unit test wise, works fine like this. Unfortunately, this breaks the actual site functionality. When I uncomment the last line of the controller, the unit test breaks, and the site works. Any help is appreciated. Thanks!

FIXED: Thanks to fiskers7's answer, this is my solution.

it('should get stuff', function () {
    var url = '/api/stuff';
    var httpResponse = [{ "stuffId": 1 }, { "stuffId": 2 }];
    httpLocalBackend.expectGET(url).respond(200, httpResponse);
    httpLocalBackend.expectGET(url).respond(200, httpResponse);
    $scope.getStuff();
    httpLocalBackend.flush();
    expect($scope.stuff.length).toBe(2);
} );

解决方案

Verbatim from my comment:

When you create the controller it makes a call to 'api/stuff' and when you call $scope.getStuff() in your test you call it again. So instead of one call to 'api/stuff' you have two which is what the error is saying. httpBackend didn't expect two calls to the endpoint, only one so it throws the error.

Code example from my comment to this answer if you need to see it.

it('should get stuff', function () {
  var url = '/api/stuff';
  var httpResponse = [{ "stuffId": 1 }, { "stuffId": 2 }];
  httpLocalBackend.expectGET(url).respond(200, httpResponse);
  httpLocalBackend.flush();
  expect($scope.stuff.length).toBe(2); 
});