7

I suck at unit testing. I really fucking suck at it.
Every time I try to write a unit test, I think to myself, what the fuck am I doing? How does this even work? What am I testing? Why do I need to new up 100 dependencies? how do I test this private behaviour from the boundaries of my object? Can I even substitute a Factory delegate? Does that even work? What if my object needs a dependency to have certain state? How do I mock that?

Does anyone have any good reading material to make me not shit at writting automated tests?

Comments
  • 0
    Okay great I see someone feels exactly like me.
  • 0
    @illusion466 you’re trying to hard. Think of each function as a unit. Each unit is supposed to fulfill part of a specification. The test should be “was the spec met?”. That’s why you write the test BEFORE you implement the function.
  • 0
    Bonus: each unit should stand alone without dependencies. That is, if they need data from a source, you mock it. Your testing ylthe unit itself, not what it depends on
  • 1
    Try writing your tests before you write your code!
  • 1
    I mean, saying these things is all good and well.
    But what about when youre adding tests to someone elses code, where the dependency chain is so deep, fixing it would require an entire rewrite of an application, a rewrite that the business wont pay for.
    And what about when certain behaviour you need to test has spilt out into the state of other objects and dependencies?
    I cant think of units as just functions anymore. Maybe im in too deep, its all a lost cause :'(
  • 0
    @illusion466 first step would be to write tests. If they fail because of the dependencies then so be it. Do your part so that you have test coverage of all the units. You may actually save the companies life
  • 0
    Just start mocking stuff. Like the thing that's so deep in calls just create mocks for everything else and set the return values for what you expect so you don't have to worry about other functions you're not testing.. I'm not saying it won't still suck but it's definitely doable.

    I get where your coming from though, I've been writing some unit test for some legacy asp.net web forms code and it sucks.
Your Job Suck?
Get a Better Job
Add Comment