-
Notifications
You must be signed in to change notification settings - Fork 142
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
td.replace('mongoose') throws exception #316
Comments
We can't hope to provide support for successfully imitating every third party module under the sun (there are a lot of goofy ways to structure JavaScript that don't clone well), and in general we don't encourage folks to directly punch-out third party libraries (see: Don't Mock What You Don't Own). That said, it would be a great help if you could try debugging this for us, both by looking at the part of mongoose that's exploding and potentially bisecting the failure against testdouble.js's repo to see if a particular commit introduced the break |
Hi, thanks for answering, I've read 'don't mock what you don't own' and I must say I disagree with the given solution of wrapping every third side library, this is just choose duplication in my opinion and even worse is writing code just to pass a test and not because it's giving us any value. |
The majority of my point below still stands, but; I was mistaken; It appears as though you can do this; let someDatabaseConstructor = td.replace('someDatabase')
let standIn = {}
td.when(someDatabaseConstructor(), {ignoreExtraArgs: true}).thenReturn(standIn) Original post below; I agree with the "Don't mock what you don't own" to some (large) degree, but what if you want to test that the code in your adapter/wrapper works without crashing, which might be the case in a dynamically typed language? And what if you are like me and really want to see 100% line coverage? I would really have loved to see some easy way to do the following: let SomeDatabase = require('someDatabase')
function myDatabaseAdapterWrapper(name, host, port){
return new SomeDatabase({port: port, host: host, name: name})
} // TestFile
let SomeDatabase = require('someDatabase')
let someDatabaseConstructor = td.replace(SomeDatabase, 'constructor')
let whateverTestObject = {}
td.when(someDatabaseConstructor({/*...*/})).thenReturn(whateverTestObject)
assert(myDatabaseAdapter('test','localhost', 1234) === whateverTestObject) This test might at first seem naive, but remember, Javascript does not necessarily notify you of typos; Eg; let SomeDatabase = require('someDatabase')
function myDatabaseAdapterWrapper(name, host, port){
return new SomeDatabaise({port: port, host: host, name: name})
} Thus, you really want to run the code once just to be extra sure. |
Addressing some of this feedback in reverse order: Unit tests cannot be the only kind of tests we write. SAFE tests and ad hoc testing are unavoidable, and should cover the "run the code once just to be extra sure" case. While I understand and respect your desire to use unit tests for this, tests are complexity, too. Each test we write brings with it a cost, and we want to make sure there's a good return on that investment. 100% code coverage via unit testing runs afoul of that same razor: are all of these tests worth the additional complexity? What is the return on investment for these tests? What unit tests can I remove without lowering the confidence I have when making changes? What unit tests are redundant with SAFE tests or ad hoc testing? This should lead me back to the original issue: [In the event that it's useful and desirable, consider explicit dependency injection (as opposed to implicit disambiguation via the |
There is one thing that I would add to @Schoonology's points above, which is that it's okay if you don't agree with us on this! There are countless "right" ways to write software, and we want to be as flexible as we can be without potentially throwing others off the trail (it's why we were hesitant to introduce So, in the interest of resolving your issue without creating a bunch of new work for the library, I would encourage you to provide your own const mongoose = td.replace('mongoose', td.object(['get', 'post', 'etc'])) I don't know what methods you're concerned with, but this would likely cover your case without actually calling |
Hi, when trying to run
td.replace('mongoose')
I get the following error:
The text was updated successfully, but these errors were encountered: