Mock RIA services

7 posts, 0 answers
  1. Stephen
    Stephen avatar
    40 posts
    Member since:
    Jul 2008

    Posted 28 Apr 2011 Link to this post

    Hi,

    I've got the following test:

            [TestMethod]
            public void PayrollViewModel_CallConstructor_DisplayNameSet()
            {
                // Arrange
                var context = Mock.Create<PersonnelDomainContext>();
                var session = Mock.Create<ISession>();
                var eventAgg = Mock.Create<IEventAggregator>();
                var viewModel = new PayrollViewModel(context, session, eventAgg);
     
                // Act
                viewModel.GetPersonnel();
     
                // Assert
                Assert.AreEqual("Personnel List", viewModel.DisplayName"Display name is incorrect");
            }

    Basically, I'm trying to Mock the DomainContext that is generated by RIA Services. I'd like all the service calls to the services on the server side to be mocked so they are not run/called.
    I'm getting the following error:

    TestMethod: PayrollViewModel_CallConstructor_DisplayNameSet
    TestClass: PayrollViewModelTests
    Result: Failed

    Exception:
    Could not create a mock from the specified target.
       at Telerik.JustMock.MockManager.CreateInstance(Type target, Container container, Boolean profilerEnabled)
       at Telerik.JustMock.MockManager.SetupMock(Type target, Behavior behavior, Boolean static)
       at Telerik.JustMock.MockManager.CreateInstance()
       at Telerik.JustMock.Mock.Create(Type target, Behavior behavior, Object[] args)
       at Telerik.JustMock.Mock.Create(Type target, Object[] args)
       at Telerik.JustMock.Mock.Create[T]()
       at PayrollPrototype.Client.Personnel.Tests.PayrollViewModelTests.PayrollViewModel_CallConstructor_DisplayNameSet()

    Failed

    I suspect it is because the DomainContext is sealed. So how do I mock it, I'm supposed to be able to mock just about anything with justmock. (pardon the pun). I've been reading about mocking for some time but am still weak in the implementation. Need more practice. I keep seeming to hit errors when I try to mock anything.

    thanks,
    Stephen

  2. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 05 May 2011 Link to this post

    Hi Stephen,

    Thanks again for the post. However, you can't mock a sealed class if profiler is not enabled or if you are running JustMock within Silverlight runtime. Since, there is no profiler in that regard.

    As the only way for mocking in Silverlight runtime is through proxy. Therefore, to mock the above class it must be a non-sealed class.

    Hope this information is helpful.


    Kind Regards,
    Ricky
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. DevCraft R3 2016 release webinar banner
  4. Stephen
    Stephen avatar
    40 posts
    Member since:
    Jul 2008

    Posted 05 May 2011 Link to this post

    So, pretending I'm a newbie at mocking, (ok don't have to pretend very hard) what would be the way to unit test or mock out the sealed class that I have as a dependency for the code I want to test? Could you give an example using the test I posted? Sorry, I'm still learning this stuff. :)

    Also will Silverlight elevated mode be a future feature coming any time soon? Intend to use JustMock in any case, but would be nice to know if its a feature coming.

    thanks,
    Stephen
  5. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 12 May 2011 Link to this post

    Hi Stephen,

    Thanks again for the reply. Generally, you need to have profiler installed and running to mock sealed classes. This is automatically done on behalf when you install JustMock full edition.

    On your second question, we do have a plan to include elevated mode support in Silverlight but that won’t be inside the SL runtime as it does not support profiling anyway, but rather you will need to mock SL library from C# test project in order to do so.

    Hope that answers your question.



    Kind Regards,
    Ricky
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  6. Stephen
    Stephen avatar
    40 posts
    Member since:
    Jul 2008

    Posted 12 May 2011 Link to this post

    Hi Ricky,

    I was surprised about your response about Silverlight not supporting profiling. I remember Silverlight 2 and 3 not supporting profiling but thought that Silverlight 4 now has it. Jetbrains and Redgate have released profilers that work with Silverlight, and so does the inbuilt Visual Studio profiler.
    I could be thinking about something totally different anyway thought I'd mention it.

    thanks for your reply.
    cheers,
    Stephen
  7. Josh
    Josh avatar
    1 posts
    Member since:
    Mar 2011

    Posted 17 May 2011 Link to this post

    how can I tell if the profiler/profiling is turned on?  I have a full install of JustMock running, but I'm getting the same error as Stephen when I try to mock a sealed class.

    I checked the Telerik menu in Visual Studio, and JustMock is enabled, but there don't seem to be any menu options (or help content) that mention profiling.
  8. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 20 May 2011 Link to this post

    Hi Josh,
    Thanks again for  bringing up the question. In case your profiler is not installed correctly or other profiling tool is overriding the target variable. In that regard you could do the following check:

    Assert.IsTrue(Mock.IsProfilerEnabled);

    Further in order to troubleshoot, you can check the current profiler version as well. For JustMock the profiler variables should look like:

    "COR_ENABLE_PROFILING", "0x1"
    "COR_PROFILER", "{D1087F67-BEE8-4f53-B27A-4E01F64F3DA8}"
    "COMPLUS_ProfAPI_ProfilerCompatibilitySetting", "EnableV2Profiler"

    You can compare this with the output from the following lines which you can run inside the vs immediate window during test debug :

    System.Environment.GetEnvironmentVariable("COR_ENABLE_PROFILING"); 
    System.Environment.GetEnvironmentVariable("COR_PROFILER");

    Kind Regards,
    Ricky
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Back to Top
DevCraft R3 2016 release webinar banner