Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

contains not working against IIFE objects #192

Closed
martin-gillen opened this issue Mar 2, 2017 · 1 comment
Closed

contains not working against IIFE objects #192

martin-gillen opened this issue Mar 2, 2017 · 1 comment

Comments

@martin-gillen
Copy link

If the subject under test and/or the arg passed to the contains() matcher was created using IIFE then the matcher is not working correctly.

Here is an example:

            it('TD contains with IIFE objects', () => {

                var brew = td.function();

                var Container = (function () {
                    function Container() {
                    }
                    return Container;
                }());

                var Coffee = (function () {
                    function Coffee() {
                        this.container = new Container();
                    }
                    return Coffee;
                }());

                var smallCoffeeContainer = new Coffee();
                smallCoffeeContainer.container.size = 'S';

                var largeCoffeeContainer = new Coffee();
                largeCoffeeContainer.container.size = 'L';

                var smallCupOfCoffee = new Coffee();
                smallCupOfCoffee.container.size = 'S';
                smallCupOfCoffee.container.type = 'cup';
                smallCupOfCoffee.ingredient = 'beans';

                console.log('smallCupOfCoffee', JSON.stringify(smallCupOfCoffee));
                console.log('smallCoffeeContainer', JSON.stringify(smallCoffeeContainer));
                console.log('largeCoffeeContainer', JSON.stringify(largeCoffeeContainer));

                td.when(brew(td.matchers.anything())).thenReturn('little steam');
                expect(brew(smallCupOfCoffee)).to.equal('little steam');
                td.verify(brew(td.matchers.contains(smallCoffeeContainer)));
        
            });

This produces:

    Error: Unsatisfied verification on test double.
  Wanted:
    - called with `(contains("[object Object]"))`.
  But was actually called:
    - called with `("[object Object]")`.
      at Object.fail (node_modules\testdouble\lib\log.js:21:13)
      at Object.module.exports [as verify] (node_modules\testdouble\lib\verify.js:28:20)
      at Context.<anonymous> (build\test\controllers\services-controller.tests.js:182:20)

The three console.log lines produced:

smallCupOfCoffee {"container":{"size":"S","type":"cup"},"ingredient":"beans"}
smallCoffeeContainer {"container":{"size":"S"}}
largeCoffeeContainer {"container":{"size":"L"}}

If I refactor the test to use objects which have been created declaratively:

            it('TD contains with simple objects', () => {

                var brew = td.function()

                let smallCoffeeContainer = {
                    container:  {
                        size: 'S'
                    }
                };

                let largeCoffeeContainer = {
                    container:  {
                        size: 'L'
                    }
                };

                let smallCupOfCoffee = {
                    container:  {
                        size: 'S',
                        type: 'cup'
                    },
                    ingredient: 'beans'
                };

                console.log('smallCupOfCoffee', JSON.stringify(smallCupOfCoffee));
                console.log('smallCoffeeContainer', JSON.stringify(smallCoffeeContainer));
                console.log('largeCoffeeContainer', JSON.stringify(largeCoffeeContainer));

                td.when(brew(td.matchers.anything())).thenReturn('little steam');
                expect(brew(smallCupOfCoffee)).to.equal('little steam'); 
                td.verify(brew(td.matchers.contains(smallCoffeeContainer)));
            });

In this case it just works. The console logs print the same.

I can make the IIFE case work by stripping the functions out of the objects before invoking the tests:

            it('TD contains with IIFE objects', () => {

                var brew = td.function();

                var Container = (function () {
                    function Container() {
                    }
                    return Container;
                }());

                var Coffee = (function () {
                    function Coffee() {
                        this.container = new Container();
                    }
                    return Coffee;
                }());

                var smallCoffeeContainer = new Coffee();
                smallCoffeeContainer.container.size = 'S';

                var largeCoffeeContainer = new Coffee();
                largeCoffeeContainer.container.size = 'L';

                var smallCupOfCoffee = new Coffee();
                smallCupOfCoffee.container.size = 'S';
                smallCupOfCoffee.container.type = 'cup';
                smallCupOfCoffee.ingredient = 'beans';

                console.log('smallCupOfCoffee', JSON.stringify(smallCupOfCoffee));
                console.log('smallCoffeeContainer', JSON.stringify(smallCoffeeContainer));
                console.log('largeCoffeeContainer', JSON.stringify(largeCoffeeContainer));

                smallCupOfCoffee = JSON.parse(JSON.stringify(smallCupOfCoffee));
                smallCoffeeContainer = JSON.parse(JSON.stringify(smallCoffeeContainer));

                td.when(brew(td.matchers.anything())).thenReturn('little steam');
                expect(brew(smallCupOfCoffee)).to.equal('little steam');
                td.verify(brew(td.matchers.contains(smallCoffeeContainer)));
        
            });

... but that doesn't help me when the subject under test is creating the IIFE.

@searls searls added this to Need to Triage in Bugs & Requests Mar 3, 2017
@searls
Copy link
Member

searls commented Mar 3, 2017

This is a fairly long example and what was going on didn't immediately jump out to me. (Given what I remember of using IIFEs, the issue is probably the result of somebody comparing something by reference, but that reference changing if the IIFE is re-invoked)

I threw triage of this issue on the backlog, but would encourage you to attempt to debug the root problem on your own if you need a quick resolution

@searls searls closed this as completed Mar 12, 2017
@searls searls moved this from Need to Triage to Done in Bugs & Requests Apr 30, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Development

No branches or pull requests

2 participants