同一字符串上的 Jasmine 错误,'Expected .. to equal ...'

Jasmine Error on same string, 'Expected .. to equal ...'

我收到此错误,但我已经检查并测试了字符串,它们完全相同:

Expected { $$state : { status : 1, value : { customerNumber : 'customerNumber', name : 'name', userId : 'buId', customerType : 'type', address : 'displayAddress' } } } 
to equal { $$state : { status : 1, value : { customerNumber : 'customerNumber', name : 'name', userId : 'buId', customerType : 'type', address : 'displayAddress' } } }.
Error: Expected { $$state : { status : 1, value : { customerNumber : 'customerNumber', name : 'name', userId : 'buId', customerType : 'type', address : 'displayAddress' } } } to equal { $$state : { status : 1, value : { customerNumber : 'customerNumber', name : 'name', userId : 'buId', customerType : 'type', address : 'displayAddress' } } }.
        at C:/customerServiceSpec.js:70

我唯一能注意到的是最后有一个句号,但我认为 Jasmine 在响应中添加了它。这是我的测试代码:

describe('CustomerService', () => {
    var mockUserSession: any = {};
    var testService any = {};
    var $q: ng.IQService;
    var createCustomerDetailsService;
    var customerDetailsService;
    var createResolvedPromise;
    var createRejectedPromise;
    var resolvePromises;

    var testResponse = {
        customers: {
            displayCustomerNumber: 'customerNumber',
            name: 'name',
            id: 'id',
            type: 'type',
            displayAddress: 'displayAddress'
        }
    };

    var serviceResponse={
        $$state: {
            status: 1,
            value: {
                customerNumber: 'customerNumber',
                name: 'name',
                id: 'id',
                customerType: 'type',
                address:'displayAddress'
            }
        }
    };

    var rootScope;

    beforeEach(() => {
        module('app.customer');

        inject(( _$q_, $injector) => {
            this.$q = _$q_;
            rootScope = $injector.get('$rootScope');

            createResolvedPromise = (result) => {
                return () => {
                    return this.$q.when(result);
                };
            };

            resolvePromises = () => {
                rootScope.$digest();
            };

            createCustomerDetailsService = () => {
                return new app.customer.CustomerService(
                    testService);
            };
        });

    });

    it('WILL search by customer ID and return a customers details', () => {
        var searchResponsePromise;

        testService.getCustomerDetails = jasmine.createSpy("getCustomerDetails").and.callFake(createResolvedPromise(testResponse));
        customerDetailsService = createCustomerDetailsService();
        searchResponsePromise = customerDetailsService.getCustomerDetails('12345678');
        resolvePromises();

        expect(searchResponsePromise).toEqual(serviceResponse);
    });
});

这是我的服务:

public getCustomerDetails(customerID:string): ng.IPromise<ICustomerDetails> {
    return this.testService.getCustomerDetails(customerID).then((customerResponse:ICustomerResult) => {

        var customer = customerResponse.customers;

        var customerDetailsResult:ICustomerDetails = {
            customerNumber: customer.displayCustomerNumber,
            name: customer.name,
            userId: customer.buId,
            customerType: customer.type,
            address: customer.displayAddress
        };

        return customerDetailsResult;
    });
}

感谢您的帮助。

这是因为 json 对象实际上是对象的不同实例,但它们包含相同的数据。你需要做这样的事情:

expect(angular.equals(searchResponsePromise, serviceResponse)).toBe(true);

有关执行此操作的首选方法,请参阅 AngularJS + Jasmine: Comparing objects。本质上,您想使用 expect(...).toEqual(...) 而不是 expect(...).toBe(...)toEqual() 进行深度比较。

与接受的答案相比,这样做的好处是 toBe(true) 的失败断言的错误将简单地说出类似于 expected false to be true 的内容,这没有足够的信息来修复问题。

toEqual() 执行的深度比较将导致包含预期对象和实际对象的断言错误,序列化为字符串。它使解决问题变得更好、更容易。 :)