This is a migrated thread and some comments may be shown as answers.

Catch-22 mocking silverlight view model

3 Answers 59 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Michael
Top achievements
Rank 1
Michael asked on 29 Jan 2015, 04:23 PM
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





3 Answers, 1 is accepted

Sort by
0
Accepted
Stefan
Telerik team
answered on 30 Jan 2015, 01:48 PM
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.

 
0
Michael
Top achievements
Rank 1
answered on 30 Jan 2015, 02:11 PM
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
0
Stefan
Telerik team
answered on 30 Jan 2015, 02:29 PM
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.

 
Tags
General Discussions
Asked by
Michael
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Michael
Top achievements
Rank 1
Share this question
or