【问题标题】:Should not working as expected不应该按预期工作
【发布时间】:2018-03-26 19:18:58
【问题描述】:

我是应该测试的新手。一直使用 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','alfa@beta.com','java');
            let valid = { profile_name: 'alfa', user_name: 'Alfa da Silva', email: 'alfa@beta.com', language: 'java'};
            profile.should.equal(valid);
        });

    });

});

但我收到以下错误:

简介 #构造函数 1) 应该返回一个 Profile 对象

0 通过 (62ms) 1 次失败

1) 简介 #构造函数 应该返回一个 Profile 对象:

 AssertionError: expected Profile {

profile_name: '阿尔法', user_name: '阿尔法达席尔瓦', 电子邮件:'alfa@beta.com', 语言:'java' } 成为对象 { profile_name: '阿尔法', user_name: '阿尔法达席尔瓦', 电子邮件:'alfa@beta.com', 语言:'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)

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

【问题讨论】:

    标签: javascript should.js assertj


    【解决方案1】:

    您必须使用 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);
            });
    
        });
    
    });
    

    【讨论】:

      【解决方案2】:

      是的。来自should documentation

      should.equal(实际,预期,[消息])

      Node.js 标准 assert.equal。

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

      使用抽象相等比较 (==) 测试实际参数和预期参数之间的浅显强制相等。

      看来您需要使用eql(..)deepEqual(..)。类似的东西

      profile.should.be.eql(valid);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-15
        • 2015-06-26
        • 2013-05-28
        • 1970-01-01
        • 2019-01-03
        相关资源
        最近更新 更多