javascriptshould.jsassertj

Should not working as expected


I am new to Should testing. Always used Assert but I'm trying new options.

This simple test is not working and I am curious to understand why.

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);
        });

    });

});

But I am getting the following error:

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)

What is wrong here? Am I missing something?


Solution

  • You have to use profile.should.match. Because the prototype of two object is different. You can get information from 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);
            });
    
        });
    
    });