locked
Unity, Unit tests and ASP.NET (not MVC) RRS feed

  • Question

  • I am using Unity in an ASP.NET project (not MVC).  It seems that the Unit Tests need to be aware of Unity, create an IoC container and insert Mocks into that, unlike the other unit testing frameworks I have used.

    The examples of resolving classes using Unity seems to be either:

        public class Foo
        {

            [Dependency]
            private ILogger Logger { set; get; }

        }

    or resolving directly from the container:

        UnityTest.Interfaces.IMenuBL ser = Global.Container.Resolve<UnityTest.Interfaces.IMenuBL>();

    Is it possible, in non-MVC ASP.NET to have Unity perform constructor injection so that my unit tests aren't tied to my IoC container?

    Cheers

    Dave

     

    Tuesday, October 29, 2013 1:51 PM

Answers

  • Hi Dave,

    I found that you have posted the same issue here:

    ASP.NET Web Forms weren't designed with testability in mind. You can't do contructor injection with Web Forms, at least not without using some dodgy workarounds like creating a page handler - see How to use Dependency Injection with ASP.NET Web Forms.

    The best option is to use property injection.

    Edit: you may consider using the MVP pattern in order to improve the testability of yout Web Forms pages:

    MSDN - Better Web Forms with the MVP Pattern

    Since it has been resolved, I will close this thread, thanks for your understanding.

    Best Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, October 31, 2013 9:45 AM

All replies

  • Hi Dave,

    I found that you have posted the same issue here:

    ASP.NET Web Forms weren't designed with testability in mind. You can't do contructor injection with Web Forms, at least not without using some dodgy workarounds like creating a page handler - see How to use Dependency Injection with ASP.NET Web Forms.

    The best option is to use property injection.

    Edit: you may consider using the MVP pattern in order to improve the testability of yout Web Forms pages:

    MSDN - Better Web Forms with the MVP Pattern

    Since it has been resolved, I will close this thread, thanks for your understanding.

    Best Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, October 31, 2013 9:45 AM
  • No problem, thanks.
    Sunday, November 10, 2013 3:48 PM