简体   繁体   English

如何使用Jasmine单元测试init()函数?

[英]How do I unit test a init() function with Jasmine?

I'm trying to write a unit test for an init function and I'm getting an error where I am calling collectionReport.init() in the test.... 我正在尝试为init函数编写单元测试,但在测试中调用collectionReport.init()时出现错误。

TypeError: undefined is not an object TypeError:未定义不是对象

This is the code I am trying to test... 这是我要测试的代码...

class CollectionsReport {
    constructor({ editCollectionsId, hasCollections}) {

    this.editCollectionsId = editCollectionsId;
    this.hasCollections = hasCollections
}

init({ id, name }) {
    this.id = id;
    this.name = name;

    // need to test this
    if (this.hasCollections) {
        this.collection = this.collections.find(c => c.staticId === 'CAR-COLLECTION');
    }
}

And this is my test so far 到目前为止,这是我的测试

describe('CollectionsReport', () => {
    const collectionArgs = {
        editCollectionsId: jasmine.createSpy(),
        hasCollections: false,
    };

    const collections = [
            {
                id: 1,
                name: 'foo',
                staticId: 'CAR-COLLECTIONS',
            },
            {
                id: 2,
                name: 'bar',
                staticId: 'TRUCK-COLLECTIONS',
            },
        ];

    let collectionReport;

    beforeEach(() => {
        collectionReport = new CollectionsReport(collectionArgs);
    });

    describe('.init()', () => {
        it('should test hasCollections', () => {
            collectionReport.init();

            //test this.hasCollections here

        });
    });
});

I'm sure its a mess, so please comment on how to fix and improve it. 我确定这是一团糟,因此请评论如何修复和改进它。

Not sure what is the purpose of the CollectionsReport class, but maybe this will lead you to the right direction: 不知道CollectionsReport类的目的是什么,但是也许这将使您朝正确的方向前进:

class CollectionsReport {
  constructor({ editCollectionsId, hasCollections}) {
    this.editCollectionsId = editCollectionsId
    this.hasCollections = hasCollections
  }

  init({ collections, staticId }) {
    this.hasCollections = !!collections.find(c => c.staticId === staticId)
  }
}

describe('CollectionsReport', () => {
  const collectionArgs = {
    editCollectionsId: jasmine.createSpy(), // Not really using it
    hasCollections: false
  }

  const collections = [
    {
      id: 1,
      name: 'foo',
      staticId: 'CAR-COLLECTIONS'
    }, {
      id: 2,
      name: 'bar',
      staticId: 'TRUCK-COLLECTIONS'
    }
  ]

  describe('.init()', () => {
    let collectionReport
    beforeEach(() => {
      collectionReport = new CollectionsReport(collectionArgs)
    })

    it('should test hasCollections', () => {
      collectionReport.init({ collections, staticId: 'CAR-COLLECTIONS' })

      expect(collectionReport.hasCollections).toBe(true)
    })

    it('should test hasCollections', () => {
      collectionReport.init({ collections, staticId: 'SOMETHING-ELSE' })

      expect(collectionReport.hasCollections).toBe(false)
    })
  })
})

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

相关问题 如何在Jasmine中单元测试服务相关功能? - How do I unit test a service-dependent function in Jasmine? Javascript单元测试Jasmine /单元测试-如何测试是否已调用私有函数? - Javascript Unittesting Jasmine / Unit Testing - how do I test whether a private function has been called? 如何使用Jasmine(Angular)单元测试下载文件? - How do I unit test downloading a file using Jasmine (Angular)? 如何在Jasmine单元测试中传递参数? - How do i pass the parameter in Jasmine Unit Test? 如何在 Jasmine 中编写单元测试到嵌套函数 - How to write unit test in Jasmine to nested function 如何在茉莉花中对以下功能进行单元测试? - How to unit test the following function in jasmine? Angular - 如何使用 jasmine 对 rxjs 6 运算符中的函数进行单元测试,使其显示为在 istanbul 中覆盖? - Angular - How do I unit test a function inside an rxjs 6 operator using jasmine so that it shows up as covered in istanbul? 如何为需要测试其发射的角度指令进行茉莉花单元测试? - How do I do a jasmine unit test for my angular directive that needs to test its emit? 我如何使用茉莉花测试与嵌套超时的javascript函数 - how do i test javascript function with nested timeouts using jasmine 如何根据茉莉花单元测试中的其他对象测试功能 - How to test function depending on other objects in jasmine unit test
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM