茉莉花测试找不到导入的变量

时间:2018-07-18 22:09:12

标签: angular typescript unit-testing jasmine

我正在使用Angular,我设置了以下类:

import { USERS } from "./data/users"; // imports an Array of Objects

export class User {
    constructor(name: string) {
        const user = USERS.find(e => e.name === name);
    }
...
}

当我编译和构建时,这工作正常。该网站按预期运行。但是,当我尝试使用Jasmine对其进行单元测试时,该测试无法找到const USERS并引发错误TypeError: Cannot read property 'find' of undefined。我的规格文件是这样的:

import { User } from './users.module';
import { USERS } from "./data/users";

describe('UserModule', () => {
  let userModule: UserModule;

  beforeEach(() => {
    userModule = new UserModule();
  });

  it('should create a user', () => {
    const user = new User("Testuser");
    expect(user).toBeTruthy();
  });
});

为什么类User不能像在生产中一样在测试中找到变量USERS?我该如何使班级找到该变量?

1 个答案:

答案 0 :(得分:0)

您发布的规范文件看起来有些偏离。您正在导入类recyclerViewSignup.setNestedScrollingEnabled(false); ,但未使用它;相反,您正在创建User的实例。这可能与它有关。