当我尝试调用任何构建器“setter”方法时,我一直遇到麻烦
这是我的代码,User.js:
class User {
constructor(userBuilder) {
this.email = userBuilder.email;
this.name = userBuilder.name;
this.surname = userBuilder.surname;
this.photo = userBuilder.photo;
this.tokens = userBuilder.tokens;
this.authorized = userBuilder.authorized;
}
getEmail() {
return this.email;
}
getName() {
return this.name;
}
getSurname() {
return this.surname;
}
getPhoto() {
return this.photo;
}
getTokens() {
return this.tokens;
}
isAuthorized() {
return authorized;
}
static get UserBuilder() {
class UserBuilder {
constructor() {
this.email = null;
this.name = null;
this.surname = null;
this.photo = null;
this.tokens = null;
this.authorized = null;
}
email(email) {
this.email = email;
return this;
}
name(name) {
this.name = name;
return this;
}
surname(surname) {
this.surname = surname;
return this;
}
photo(photo) {
this.photo = photo;
return this;
}
tokens(tokens) {
this.tokens = tokens;
return this;
}
authorized(authorized) {
this.authorized = authorized;
return this;
}
build() {
return new User(this);
}
}
return UserBuilder;
}
}
module.exports = User
我的UserTest.spec.js:
process.env.NODE_ENV = 'test'
//Import User class
let User = require('../../../../main/com/auth/user/User');
// Import chai
let chai = require('chai');
// Tell chai that we'll be using the "should" style assertions.
chai.should();
describe('User', () => {
let user;
var email = "a@a";
var name = "a";
var surname = "a";
var photo = "p";
var authorized = true;
beforeEach(() => {
//Create new User object before each test
user = new User.UserBuilder()
.email(email)
.name(name)
.surname(surname)
.photo(photo)
.authorized(authorized)
.build();
});
it('returns the email', () => {
user.getEmail().should.equal(email)
});
it('returns the name', () => {
user.getName().should.equal(name)
});
it('returns the surname', () => {
user.getSurname().should.equal(surname)
});
it('returns the photo', () => {
user.getPhoto().should.equal(photo)
});
it('returns the authorized', () => {
user.getAuthorized().should.equal(authorized)
});
})
我得到TypeError:user.email不是函数。
我尝试过用户类以外的UserBuilder:
在User.js中
module.exports = {User: User, UserBuilder: UserBuilder}
在UserTest.spec.js
中let {User} = require('../../../../main/com/auth/user/User');
let {UserBuilder} = require('../../../../main/com/auth/user/User');
user = new UserBuilder()
.email(email)
.name(name)
.surname(surname)
.photo(photo)
.authorized(authorized)
.build();
结果相同,我做错了什么?我可以调用build方法,但我不能调用任何其他方法。