【问题标题】:inject custom service in jasmine test在茉莉花测试中注入自定义服务
【发布时间】:2017-01-04 03:17:25
【问题描述】:

我为学校编写了一个 yathzee 游戏,现在我需要对我的计算进行单元测试。 我尝试注入我的服务,但它是未定义的。尝试了很多东西,真的不知道如何设置它。我很高兴业力开始工作。 所以这是我的测试文件

describe('Users factory', function() {
beforeEach(module('yathzeeCalculateService'));

var myservice;

beforeEach(inject('yathzeeCalculateService', function(yathzeeCalculateService){
  myservice = yathzeeCalculateService;
}));


describe('Test chance', function() {
  it('should be defined', function(){
    expect(myservice).toBeTruthy();
  });
    it('good count', function(){
        var dices = [1,1,1,1,1];
        expect(myservice.sum(dices)).toEqual(5);
    });
});
});

这是我的服务

(function () {
    angular.module('myApp').factory('yathzeeCalculateService', yathzeeCalculateService);
yathzeeCalculateService.$inject = ['_'];

function yathzeeCalculateService(_) {

   ... 
    return {

        sum : sum,
        getStraight : getStraight,
        isSmallStraight : isSmallStraight,
        isLargeStraight : isLargeStraight,
        getSameCount : getSameCount,
        manyOfAKind : manyOfAKind,
        getAmountOfArrayNumber : getAmountOfArrayNumber,
        threeOfAKind : threeOfAKind,
        fourOfAKind : fourOfAKind,
        yahtzee : yahtzee,
        fullHouse : fullHouse
    };
}
})();

我对数组函数使用下划线,是否必须在测试中注入?如果,如何?

【问题讨论】:

    标签: angularjs node.js jasmine karma-jasmine angular-mock


    【解决方案1】:

    模块名称错误,模块名称应该是正确的

    无效的模块名称

    beforeEach(module('yathzeeCalculateService'));
    

    你必须使用

    beforeEach(module('myApp'));
    

    【讨论】:

    • 做到了,但现在我得到“参数'fn'不是函数,得到字符串”。当我过去遇到那个错误时,有一个 ']' 放错了位置并且我的代码没有工作,但是现在我的应用程序运行良好,但是那些测试......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-13
    • 2020-06-30
    相关资源
    最近更新 更多