我使用的是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。我的spec文件如下所示:
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,就像在生产中一样?为了让类找到那个变量,我能做些什么?
https://stackoverflow.com/questions/51411684
复制相似问题