Catch-22 mocking silverlight view model

4 posts, 1 answers
  1. Michael
    Michael avatar
    26 posts
    Member since:
    Oct 2014

    Posted 29 Jan 2015 Link to this post

    Hello,

    I am trying to use JustMock on a view-model in a silverlight application.  The structure of the solution is basically as follows:

    Solution
        SilverlightApp.proj
            - ViewModels
                ViewModelX.cs
            -Views
                ViewX.xaml
                
         SIlverligthApp.Tests.proj
             ViewModelXFixture.cs

             
    Both projects are Silverlight Class projects with Silverlight 5.  The test project is using NUnitLite 1.0  libraries (also tried it with NUnit3.0) and has a reference to Telerik.JustMock.Silverlight.dll.  I have a license for the commercial version but have not enabled the profiler.  A sample unit test is below:

    [TestFixture]
    public class Class1Fixture
    {
      [Test]
      public void Len_Returns_Length()
      {
        //Arrange
        var mockDummy = Mock.Create<IDummy>();
        var x = new Class1(mockDummy);
     
        //Act
        var result = x.Len("test");
     
        //Assert
        Assert.AreEqual(4, result);
      }
    }

    And this is what I tried testing:

    public interface IDummy
    { }
     
    public class Class1
    {
      public Class1(IDummy dummy)
      { }
      public int Len(string input)
      {
        if (string.IsNullOrWhiteSpace(input))
          return 0;
        return input.Length;
      }
    }

    But this is the error I get:

    Tests1.Class1Fixture.Len_Returns_Length:
    System.TypeInitializationException : The type initializer for 'Telerik.JustMock.Mock' threw an exception.
      ----> System.InvalidOperationException : Telerik.JustMock.Silverlight should only be used inside the Silverlight runtime. For all other runtimes reference Telerik.JustMock instead.

    But since the test project is a SilverLight class library, I cannot add a reference to Telerik.JustMock.dll.

    I'm sure I'm missing something but just can recognize it at the moment.  Any advice would be much appreciated.

    Thanks,

    Mike





  2. Answer
    Stefan
    Admin
    Stefan avatar
    198 posts

    Posted 30 Jan 2015 Link to this post

    Hi Michael,

    If you're running your tests in the full .NET framework, then JustMock expects that your unit test project will also target the full .NET framework and not Silverlight. Simply retarget your test project to the full runtime.

    Telerik.JustMock.Silverlight.dll is meant to be run in the Silverlight runtime, i.e. using the Silverlight Unit Test Framework from the Silverlight Toolkit and maybe the StatLight. If  you want to run your tests in the full runtime, then you need to reference Telerik.JustMock.dll. In this case it doesn't matter to you whether your test project targets Silverlight or the full runtime and to make things work you should always target the full runtime.

    I hope this helps. Do you have a particular reason to target Silverlight in the unit test project?

    Regards,
    Stefan
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  3. DevCraft R3 2016 release webinar banner
  4. Michael
    Michael avatar
    26 posts
    Member since:
    Oct 2014

    Posted 30 Jan 2015 in reply to Stefan Link to this post

    Stefan,

    Unfortunately, I am forced to used Silverlight test projects since the project I'm assigned to has a large number of dependencies that are Silverlight only.  Time to refactor!

    Thanks for your quick reply.

    Mike
  5. Stefan
    Admin
    Stefan avatar
    198 posts

    Posted 30 Jan 2015 Link to this post

    Hi Michael,

    Unless you have regular Silverlight assemblies that reference the unit test projects themselves, you should not have a problem to reference Silverlight assemblies from full .NET assemblies and then you will have to convert only your unit test projects.

    Either way, I was happy to help. :)

    Regards,
    Stefan
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
Back to Top