I want to know About Expectations & Mocking

Hey everyone,

I am new to TestBox & diving into it after working with JavaScript testing tools such as Jest and Mocha. I have been checking out the docs and some sample projects but I still have some confusion— when it comes to writing expectations & using mocking properly in ColdBox modules.

I am doing a Java Online Training course so my brain is kind of juggling different syntax and patterns right now :sweat_smile:. I check this post testbox 2.2 mocking a DI method I appreciate if anyone could share a few best practices, simple code examples or even a quick explanation of how you approach writing test specs with TestBox.

Any tips on structuring tests & handling dependencies in a clean way would be super helpful.

Thank you…:slight_smile:

It might be more beneficial to you if you have an example test you want to go through.

You will find a lot of opinions on testing. The following are mine. :wink:

I avoid mocking when I can. Mocking is useful, but can be hard to wrap your mind around it. It can also be a glorified spell checker since with mocking you are basically saying “my code will call this method and get this back.” If the class with the method you are mocking changes, your test may still pass (depending on how it was mocked) and is no longer protecting you.

I prefer to start with integration tests, since I want to make sure the entire app is working, not just one piece. I will create unit tests as needed when making the pieces of the integration test work needs it. (Note: Integration tests can be done in a few ways. I prefer using ColdBox’s testing helpers.)

Happy to provide more examples. You can also check out different open source libraries. qb, a query builder I built, has over 1000 tests you can check out. ColdBox and TestBox itself both have test suites to look at as well.

If you are more of an audiovisual learner, check out CFCasts and search for “test”.

2 Likes