Base solution for your next web application
Open Closed

Testing {Project}.Core, Disposed Object #3337


User avatar
0
ribbo created

Hi, When trying to run a unit test directly on the domain layer {Project}.Core manager class, if the class tries to access a repository I get an error "Message: System.ObjectDisposedException : Cannot access a disposed object. "

If I instead add a call to the same manager function from the Application Layer {Project}.Application and call the application layer from the unit test it all works. Ie. If the manager has been created via the Application layer instead of within the Unit test all is fine.

Is there a way I should setup my test differently if directly call with .Core manager classes? Think it might be to do with the Unit Of Work disposing of it somehow?

All sample tests I can find samples of are doing it to the {Project}.Application layer.

Thanks, Matt


1 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Domain services are not unitOfWork by default. So when you use them in app service, they are in unitOfWork but when you use them directly in test methods, they are not in unitOfWork.

    You can either create a unitOfWork in your test method using UnitOfWorkManager or you can mark your domain service methods with unitOfWork attribute. You can check this document for more info <a class="postlink" href="https://aspnetboilerplate.com/Pages/Documents/Unit-Of-Work">https://aspnetboilerplate.com/Pages/Doc ... it-Of-Work</a>

    Thanks.