如何Jasmine测试Object.constructor的值并减少它在语句中的重复 - AngularJS

时间:2017-01-21 20:36:56

标签: javascript angularjs unit-testing jasmine

我在AngularJS应用程序中定义了一个对象模型......它看起来像这样:

.factory('Watermark', function () {

    // Constructor, with class name
    // Assumption: that backend gives us a topic or not!
    function Watermark(content, title, author, topic) {
        this.content = content;
        this.title = title;
        this.author = author;
        if(topic) {
            this.topic = topic;
        }
    }

    // Example Public method, assigned to prototype
    Watermark.prototype.getDocumentType = function () {
        return this.content;
    };

    return Watermark;
});

现在,我希望使用jasmine测试这个,我想确定当我创建一个对象的新实例时,它constructor具有我使用的函数值(在本例中为Watermark)。我目前的测试失败(见下面的xit)......

describe('Service: Watermark', function () {

    // load the module / APP
    beforeEach(module('MyAppName'));

    var Watermark;

    beforeEach(inject(function (_Watermark_){
        Watermark = _Watermark_;

        setTimeout(function(done) {
            done();
        }, 1);

    }));

    it('should be defined', function () {
        expect(Watermark).toBeTruthy();
    });

    describe('when defining a watermark with content "Book"', function () {

        it('should be an instance of "Watermark"', function () {
            var waterMark = new Watermark('Book','When boyfriends aint so super','Mary Jane','Doomed Romance');

            expect(waterMark instanceof Watermark).toBeTruthy();
        });

        //THIS FAILS!!!
        xit('should have a constructor property "Watermark"', function () {
            var waterMark = new Watermark('Book','When boyfriends aint so super','Mary Jane','Doomed Romance');
            console.log(waterMark.constructor);
            expect(waterMark.constructor).contains('Watermark');
        });

        it('should contain a content property with value "book"', function () {
            var waterMark = new Watermark('Book','When boyfriends aint so super','Mary Jane','Doomed Romance');

            expect(waterMark).toEqual(jasmine.objectContaining({
                content: 'Book'
            }));
        });

        it('should contain a title property with value "When boyfriends aint so super"', function () {
            var waterMark = new Watermark('Book','When boyfriends aint so super','Mary Jane','Doomed Romance');

            expect(waterMark).toEqual(jasmine.objectContaining({
                title: 'When boyfriends aint so super'
            }));
        });

        it('should contain a author property with "Mary Jane"', function () {
            var waterMark = new Watermark('Book','When boyfriends aint so super','Mary Jane','Doomed Romance');

            expect(waterMark).toEqual(jasmine.objectContaining({
                author: 'Mary Jane'
            }));
        });

        it('should contain a topic property with "Doomed Romance"', function () {
            var waterMark = new Watermark('Book','When boyfriends aint so super','Mary Jane','Doomed Romance');

            expect(waterMark).toEqual(jasmine.objectContaining({
                topic: 'Doomed Romance'
            }));
        });
    });

    describe('when defining a watermark with content "journal"', function () {

        it('should not contain a topic property', function () {
            var waterMark = new Watermark('Journal','My pretty lousey life as SpiderMan', 'Peter Parker');

            expect(Object.keys(waterMark)).not.toContain('topic');
        });
    });
});

我收到错误 TypeError:undefined不是构造函数我不确定我在这里做错了什么?

也可以减少我在it('....

中声明新实例的重复次数

非常感谢提前。

1 个答案:

答案 0 :(得分:0)

我认为您应该使用toContains()匹配器功能(而不是contains())。 undefined is not a constructor错误可能是因为测试浏览器无法找到contains()函数的定义 - 我可以看到在这种情况下这会让你失望。

要回答问题的第二部分,您可以避免在所有测试之前通过在var waterMark块中声明一次,然后在{{1}内设置describe来重复定义beforeEach()功能。