Win10 UWP, Prism, IoC and UnitTests



  • Okay, this may be a bit obscure but maybe someone here has the right idea. I think I finally have gotten the hang of DependencyInjection / Inversion of Control (or at least the beginnings of it) and thus wanted to leverage it to write proper UnitTests for my app.

    I'm using the Prism / Unity combination and everything works so far. I've got my views, the ViewModels get instantiated automatically, everything's fine.

    But upon trying to insert a UnitTest project, it hits a bit of a snag: As soon as the App class is not inheriting Application but rather from PrismUnityApplication, the unit testing suite crashes hard. This may not be Prism's fault, some people reported that inheriting from anything but Application yields this problem.

    Now, there's a workaround: Pull everything you want to UnitTest into a ClassLibrary and simply reference this ClassLibrary from the main app. You can then also reference only this class library in the UnitTest suite and that works as well.

    However, this time I hit another problem: If I put the ViewModels into this class library, the IoC container does not seem to be able to resolve them anymore, even though the namespace and everything else is still the same. I also don't get an error code - the app simply silently does nothing.

    Manually doing something like Container.RegisterInstance(...) also did not change anything.

    Does anyone have a clue as to what I'm doing wrong?


  • Discourse touched me in a no-no place

    @Rhywden said:

    the unit testing suite crashes hard

    This being a managed environment, I'd assume there was a stack trace available when the crash is unwound. Does that provide any information that might be relevant in helping people help you?



  • @dkf said:

    @Rhywden said:
    the unit testing suite crashes hard

    This being a managed environment, I'd assume there was a stack trace available when the crash is unwound. Does that provide any information that might be relevant in helping people help you?

    Yes, the issue is deemed Not-Prism-related but rather a general problem. Normally the App.xaml.cs starts out like this:

    sealed partial class App : Application
    

    and Unit Testing on this will work. However, as soon as you inflict inheritance upon the whole thing, it will crash. Even when you do something like this:

     class MyNewApp : Application { }
    
     sealed partial class App : MyNewApp { ... }
    

    In essence, this is probably something Microsoft needs to fix.

    To clarify the issue: I need to know how to get the workaround to ... uh, work.



  • Okay, it seems to be something with the AutoWiring that goes haywire because this piece of code works, regardless whether the viewmodel resides in the main app or the class library:

    var x = Container.Resolve(typeof(ExamplePageViewModel)) as ExamplePageViewModel;
    

    So, it can find the stuff. It just doesn't. Great.



  • I can't help you, but I'm still going to complain that there are like a MILLION software products named "Unity". During most of your post, I thought you meant Unity the game engine, until I started Googling it and apparently there some other library that does shit so abstracted away from the solution of real practical problems I can't even figure it out.



  • Okay, so I got word from the developer. My workaround "will run into issues due to the way Prism loads stuff from assemblies" (yes, I noticed that) but I could simply do this:

    [Bindable]
    sealed partial class App : MyNewApp { ... }
    

    And suddenly it works. Nice. How do I suggest without snark that this piece of information would be really helpful for others, considering that Making UnitTests Easy is somewhat a posterchild for IoC?



  • This post is deleted!

Log in to reply