It is my first time to make a Unit testing so i'm trying to find references to how to make a Unit testing of a DAO. Can you guys make a simple example of the setUpBeforeClass
, setUp
and how to test a method that inserting new data in database using model for this
. Just a simple example using easy mock. Thank you for your consideration
问题:
回答1:
The idea of using mock objects to perform your unit testing strikes me as peculiar as all you are doing is testing your mock objects instead of your real ones. If you think you need to use mock objects to emulate database access then your entire architecture is wrong. I personally build all my software using the 3-Tier Architecture where I can have as many objects as I like in the Business layer, but only 1 object in the Data Access layer. Thus if I wanted to exchange real database access with dummy database access where would I make the change? All 200+ objects in my Business layer, or just the 1 object in the Data Access layer? Why should I then implement a mechanism to change every object within my application when all I really need to do is change one?
Controllers are meant to be integration tested, not unit tested. But the testing pyramid prescribes that the unit level is where the focus should be, so people are sucked into that by default.
Assertions should never fail under any circumstance. If they fail in your tests, it indicates a logical error. Basically, if your function is doing "assert( 0 )" instead of returning an error code, then the function should be re-written.
Can you make massive changes to a class definition — even throw out the whole thing and replace it with a completely different implementation — without impacting any of the code that uses that class's objects?
As has unfortunately happened with a variety of patterns that originate from rigid languages like Java, Dependency Injection has spread and been advocated as a cross-language best practice on trumped up benefits of flexibility and malleability.
References
Dependency Injection is Evil - Tony Marston
Test Induced Design Damage - David Heinemeier Hansson
TDDing Getters and Setters Considered Harmful
Why Getter and Setter Methods are Evil
Dependency injection is not a virtue in Ruby (DHH)
TDD the RITE Way – JavaScript Scene – Medium
5 Common Misconceptions About TDD & Unit Tests – JavaScript Scene – Medium
Ned Batchelder: tl;dw: Stop mocking, start testing
ACCU :: Mocks are Bad, Layers are Bad
Google Testing Blog: Testing on the Toilet: Don’t Overuse Mocks