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

Add support for transferring negation to custom properties #69

Open
lootek opened this issue Sep 2, 2016 · 1 comment
Open

Add support for transferring negation to custom properties #69

lootek opened this issue Sep 2, 2016 · 1 comment

Comments

@lootek
Copy link

lootek commented Sep 2, 2016

An example:

Let loggedIn be defined as follows:

chakram.addProperty('loggedIn', function(response) {
   expect(response).to.have.cookie('session');
});

this works as expected (passes when there is a cookie, fails otherwise): expect(response).to.be.loggedIn;

But when negated, it still always checks for the cookie existence, ie. expect(response).to.not.be.loggedIn;
still fails when the cookie is missing, passes when it's present

The assertion from custom property used directly works both normally and negated as expected: expect(response).to.have.cookie('session');
expect(response).to.not.have.cookie('session');

@kenblair
Copy link

kenblair commented Nov 16, 2016

I'm somewhat new to Chai but it looks like an anti-pattern to me. It seems to me the pattern you're following is creating a new assertion with the same object. Instead it should be using the assertion the function was called for. It should be as simple as changing expect(response) in your function to this.

Here's an example to demonstrate.

function loggedInBroken(response) {
    expect(response).to.have.cookie('loggedIn', 'yes');
}

function loggedInWorking() {
    this.to.have.cookie('loggedIn', 'yes');
}

describe('Chakram plugins', function () {

    var withGoodCookie = chakram.get('http://httpbin.org/cookies/set?loggedIn=yes');
    var withBadCookie = chakram.get('http://httpbin.org/cookies/set?loggedIn=no');
    var withNoCookie = chakram.get('http://httpbin.org/get');

    it('should work with loggedInWorking', function () {
        chakram.addProperty('loggedIn', loggedInWorking);
        expect(withGoodCookie).to.be.loggedIn;
        expect(withBadCookie).to.not.be.loggedIn;
        expect(withNoCookie).to.not.be.loggedIn;
        return chakram.wait();
    });

    it('should work with loggedInBroken', function () {
        chakram.addProperty('loggedIn', loggedInBroken);
        expect(withGoodCookie).to.be.loggedIn;
        expect(withBadCookie).to.not.be.loggedIn;  // nope
        expect(withNoCookie).to.not.be.loggedIn; // nope
        return chakram.wait();
    });
});

Note that you will run into similar issues with other plugins such as json for the very same reasons.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants