[英]Jasmine test cannot find imported variable
I'm using Angular, I have the following class set up: 我正在使用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);
}
...
}
This is working fine when I compile and build. 当我编译和构建时,这工作正常。 The website runs as intended.
该网站按预期运行。 But when I try to unit test it with Jasmine, the test cannot find the const
USERS
and throws an error TypeError: Cannot read property 'find' of undefined
. 但是,当我尝试使用Jasmine对其进行单元测试时,该测试无法找到const
USERS
并引发错误TypeError: Cannot read property 'find' of undefined
。 My spec file is like this: 我的规格文件是这样的:
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();
});
});
Why can't the class User find the variable USERS in testing just like it can in production? 为什么User类无法像在生产中那样在测试中找到变量USERS? What can I make for the class to find that variable?
我该如何使班级找到该变量?
The spec file you posted looks a bit off. 您发布的规范文件看起来有些偏离。 You're importing the class
User
but it's not being used; 您正在导入
User
类,但未使用它; instead you're creating an instance of UserModule
. 相反,您正在创建
UserModule
的实例。 That could have something to do with it. 那可能与它有关。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.