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

Allow renaming mock functions #473

Closed
tgr opened this issue Apr 23, 2014 · 2 comments
Closed

Allow renaming mock functions #473

tgr opened this issue Apr 23, 2014 · 2 comments

Comments

@tgr
Copy link

tgr commented Apr 23, 2014

Suppose I have a test for a class that does things:

function ClassThatDoesThings(){}
ClassThatDoesThings.prototype.doThings = function() {...}
ClassThatDoesThings.prototype.doMoreThings = function() {...}
ClassThatDoesThings.prototype.doAllTheThings = function() {
    this.doThings();
    this.doMoreThings();
}

var thinger = new ClassThatDoesThings(),
    mock = sinon.mock(thinger);
mock.expect('doThings').once();
mock.expect('doMoreThings').once();
thinger.doAllTheThings();
mock.verify();

The class then becomes too complex and I use composition to split it up. The original class is now sort of a mediator and I want to verify it forwards the calls correctly.

function ClassThatDoesBasicThings(){}
ClassThatDoesBasicThings.prototype.do = function() {...}

function ClassThatDoesMoreThings(){}
ClassThatDoesMoreThings.prototype.do = function() {...}

function ClassThatDoesThings( basic, more ) {
    this.basic = basic;
    this.more = more;
}
ClassThatDoesThings.prototype.doAllTheThings = function() {
    this.basic.do();
    this.more.do();
}

var basicThinger = new ClassThatDoesBasicThings(),
    moreThinger = new ClassThatDoesMoreThings(),
    basicMock = sinon.mock(basicThinger),
    moreMock = sinon.mock(moreThinger),
    thinger = new ClassThatDoesThings( basicThinger, moreThinger );
basicMock.expect('do').once();
moreMock.expect('do').once();
thinger.doAllTheThings();
mock.verify();

Suddenly the error messages thrown by mock.verify() are not so useful since both functions are called do(). There should be a way to supply a different name, maybe

basicMock.expect('do', 'basic.do').once();

or

basicMock.expect('do').named('basic.do').once();

(I understand that there is supposed to be only one mock per test; but that kind of goes against how mocks are normally used. It does not seem reasonable to force a completely different syntax on tests because of an internal refactoring that should have no visible effects.)

@mroderick
Copy link
Member

I think it's a bit of a code smell, if you have to supply a readable name to a test library, instead of just giving your methods descriptive names.

(This reminds me of Execution in the Kingdom of Nouns)

Can you give a less contrived example of a problem that the proposed solution would solve?

@fearphage
Copy link
Member

I'm closing it this issue as it has either been addressed or is no longer an issue. If you think that is not the case, please feel free to reopen.

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

3 participants