不应该按预期工作

Should not working as expected

我是 Should 测试的新手。一直使用断言,但我正在尝试新的选项。

这个简单的测试不起作用,我很想知道为什么。

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','alfa@beta.com','java');
            let valid = { profile_name: 'alfa', user_name: 'Alfa da Silva', email: 'alfa@beta.com', language: 'java'};
            profile.should.equal(valid);
        });

    });

});

但我收到以下错误:

Profile #constructor 1) should return a Profile object

0 passing (62ms) 1 failing

1) Profile #constructor should return a Profile object:

 AssertionError: expected Profile {

profile_name: 'alfa', user_name: 'Alfa da Silva', email: 'alfa@beta.com', language: 'java' } to be Object { profile_name: 'alfa', user_name: 'Alfa da Silva', email: 'alfa@beta.com', language: 'java' } + expected - actual

 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)

这里有什么问题?我错过了什么吗?

是的。来自 should documentation

should.equal(actual, expected, [message])

Node.js standard assert.equal.

Nodejs documentation 我们知道 assert.equal(...)

Tests shallow, coercive equality between the actual and expected parameters using the Abstract Equality Comparison ( == ).

看来您需要使用 eql(..)deepEqual(..)。像

profile.should.be.eql(valid);

你必须使用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','alfa@beta.com','java');
            let valid = { profile_name: 'alfa', user_name: 'Alfa da Silva', email: 'alfa@beta.com', language: 'java'};
            profile.should.match(valid);
        });

    });

});