0

I'm getting this error, but I've checked and tested the strings and they are exactly the same:

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

The only thing I can notice is there is a full stop at the end, but I think Jasmine is adding that in the response. Here is my test code:

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

And here is my service:

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

Thanks for any help.

Desmond
  • 1,656
  • 3
  • 22
  • 34
  • I'm confused too, but I don't see the `status` field in the return of your getCustomerDetails() function, even though it surely appears in the Jasmine output – Gabriel Pires Sep 23 '15 at 04:04
  • It may happen that you key is not getting identified as it contains special characters please try adding double or single quotes and try again. – Jayant Patil Sep 23 '15 at 05:55
  • Thanks for your help, Gabriel and Jayant – Desmond Sep 23 '15 at 07:34

2 Answers2

3

This happens due to the fact that json objects are actually different instances of objects, but they contain the same data. You need to do something like this:

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

Diana R
  • 1,174
  • 1
  • 9
  • 23
-1

See AngularJS + Jasmine: Comparing objects for the preferred way of doing this. Essentially, you want to use expect(...).toEqual(...) instead of expect(...).toBe(...). toEqual() performs a deep comparison.

The advantage to doing this over the accepted answer is the error for a failed assertion with toBe(true) will simply say something along the lines of, expected false to be true, which isn't enough information to fix the problem.

The deep comparison performed by toEqual() will result in an assertion error containing both the expected and actual objects, serialized as strings. It makes it much nicer and easier to fix the problem. :)

Community
  • 1
  • 1
Ben H
  • 31
  • 5