locked
ioc for testability? RRS feed

  • Question

  • User1610691276 posted
    Hello, I'm trying to understand how ioc supports better testability. For example, let's say I have a UserBll with a CreateUser method and a UserDal with a related CreateUser method.

    I could create a unit test method that calls CreateUser on UserBll. I could also create a separate unit test method that calls CreateUser on UserDal. So how would injecting UserDal as an IDal into UserBll support better testability over what I described in my first paragraph?
    Wednesday, July 23, 2014 12:22 AM

Answers

  • User-821857111 posted

    You should have an IUserDal which specifies a CreateUser method. Then you can develop both a UserDal and a FakeUserDal that implement that interface. The UserDal (which communicates with the database) is injected at runtime and the FakeUserDal (which fulfills the interface contract by returning mocked up objects) is injected at test time. It stops the unit test having to make an actual call to a database which can slows tests down.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 23, 2014 2:08 AM