我是应该测试的新手。一直使用Assert,但我正在尝试新的选项。
这个简单的测试无法正常工作,我很想知道为什么。
Profile.js
class Profile {
constructor(profile_name,user_name,email,language) {
this.profile_name = profile_name;
this.user_name = user_name;
this.email = email;
this.language = language;
}
}
module.exports = Profile;
Profile_test.js
let should = require('should');
let Profile = require('../lib/entities/Profile');
describe('Profile', function() {
describe('#constructor', function() {
it('should return a Profile object', function() {
let profile = new Profile('alfa','Alfa da Silva','[email protected]','java');
let valid = { profile_name: 'alfa', user_name: 'Alfa da Silva', email: '[email protected]', language: 'java'};
profile.should.equal(valid);
});
});
});
但是我收到以下错误:
轮廓
#构造函数
1)应返回一个Profile对象
0通过(62ms)
1个失败
1)个人资料
#构造函数
应该返回一个Profile对象:
AssertionError: expected Profile {
profile_name:“ alfa”,
user_name:“ Alfa da Silva”,
电子邮件:“ [email protected]”,
语言:“ java”
}成为对象{
profile_name:“ alfa”,
user_name:“ Alfa da Silva”,
电子邮件:“ [email protected]”,
语言:“ java”
}
+预期-实际
at Assertion.fail (node_modules/should/cjs/should.js:275:17)
at Assertion.value (node_modules/should/cjs/should.js:356:19)
at Context.<anonymous> (test/Profile_test.js:12:19)
怎么了我想念什么吗?
最佳答案
您必须使用profile.should.match。因为两个对象的原型不同。您可以从here获取信息。
let should = require('should');
let Profile = require('../lib/entities/Profile');
describe('Profile', function() {
describe('#constructor', function() {
it('should return a Profile object', function() {
let profile = new Profile('alfa','Alfa da Silva','[email protected]','java');
let valid = { profile_name: 'alfa', user_name: 'Alfa da Silva', email: '[email protected]', language: 'java'};
profile.should.match(valid);
});
});
});