【问题标题】:Unit Testing a service with $http and $q using mocha, chai and sinon使用 mocha、chai 和 sinon 对 $http 和 $q 服务进行单元测试
【发布时间】:2015-05-16 17:21:42
【问题描述】:

我有这个功能,需要用mocha、chai和sinon测试一下。

这是my code

service-wiki-pelis.js

'use strict'

var WikiService = function($http,$q){

    return {
        getMovies: getMovies
    }

    function getMovies() {
        var deferred = $q.defer();
        $http.jsonp('/url/movies')
            .success(function(data,status,headers,config) {
                deferred.resolve({
                    results: data.results
                });
            }).error(function(msg,code) {
                msg = msg || 'Request failed: ';
                deferred.reject(msg);
            });
        return deferred.promise;
    }

}

module.exports = WikiService;

service-wiki-pelis_test.js

var chai = require('chai'),
    should = chai.should(),
    sinon = require('sinon');

describe('Wiki Pelis service',function(){

    'use strict';

    var service, WikiPelisService = require('./service-wiki-pelis');

    var $http = {jsonp:function(url){
            return {
                success: function () {
                },
                error: function () {
                }
            }
        }},
        $q = {};

    beforeEach(function(done){
        service = new WikiPelisService($http,$q);
        done();
    });

    it('Should have a function named "JSONP"',function(){
        service.jsonp.should.be.instanceof(function);
    });
});

目前为止我做了这个小测试,但是真的不行,但是有了它你就可以看到我的测试环境了。

这两个文件是 Angular 应用程序的一部分,但应该可以单独测试这些文件,无需 Angular 模拟或类似的东西。

【问题讨论】:

  • 请在您的帖子中包含所有相关代码,并且不要仅包含指向代码托管站点的链接。您的帖子应该独立于任何其他资源;考虑一下如果该网站将来出现故障会发生什么!
  • @dcodesmith 我已经添加了我的测试样本,希望你能帮助我。

标签: angularjs unit-testing browserify sinon chai


【解决方案1】:

我正在使用 jasmine,所以我可以为解决方案创建一个 jsfiddle。在行中查找 cmets。

您需要 angular-mocks 文件。您仍然可以使用 chai 进行断言。

describe('Service: ', function () {
    var service,
        $httpBackend,
        url = '/url/movies',
        returnedPromise,
        result;

    //load the angular app module
    beforeEach(function () {
        module('MyApp');
    });
    // inject your service and $httpBackend from angular-mocks.js
    beforeEach(inject(function (MyService, _$httpBackend_) {
        service = MyService;
        $httpBackend = _$httpBackend_;
        // invoke your getMovies method
        returnedPromise = service.getMovies();
    }));

    //make sure no expectations where missed
    afterEach(function () {
        $httpBackend.verifyNoOutstandingExpectation();
        $httpBackend.verifyNoOutstandingRequest();
    });

    it('should make a successful API call and return a list of movies', function () {
        var expectedResult = {
            results: []
        };
        $httpBackend.whenJSONP(url).respond({
            results: []
        });
        returnedPromise.then(function (response) {
            result = response;
        });
        $httpBackend.flush();
        expect(result).toEqual(expectedResult);
    });

    it('should make a unsuccessful API call and return an error', function () {
        var expectedResult = 'Request failed: ';
        $httpBackend.whenJSONP(url).respond(401);
        returnedPromise.then(function (response) {}, function (msg) {
            result = msg;
        });
        $httpBackend.flush();
        expect(result).toEqual(expectedResult);
    });
});

JSFIDDLE

【讨论】:

  • 提前谢谢,但我真的不想使用“angular-mocks”,使用这种方式存在很多教程,而且确实是更简单的方法,我想使用 sinonjs 来模拟依赖关系。
猜你喜欢
  • 2016-05-26
  • 2021-09-27
  • 2019-02-14
  • 2017-06-09
  • 1970-01-01
  • 2015-10-01
  • 2023-04-11
  • 2015-12-01
  • 2020-10-11
相关资源
最近更新 更多