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

Mock RIA services

6 Answers 116 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Stephen
Top achievements
Rank 2
Stephen asked on 28 Apr 2011, 02:03 PM
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

6 Answers, 1 is accepted

Sort by
0
Ricky
Telerik team
answered on 05 May 2011, 01:07 PM
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
0
Stephen
Top achievements
Rank 2
answered on 06 May 2011, 02:02 AM
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
0
Ricky
Telerik team
answered on 12 May 2011, 01:55 PM
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
0
Stephen
Top achievements
Rank 2
answered on 12 May 2011, 03:31 PM
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
0
Josh
Top achievements
Rank 1
answered on 17 May 2011, 08:25 PM
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.
0
Ricky
Telerik team
answered on 20 May 2011, 03:44 PM
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
Tags
General Discussions
Asked by
Stephen
Top achievements
Rank 2
Answers by
Ricky
Telerik team
Stephen
Top achievements
Rank 2
Josh
Top achievements
Rank 1
Share this question
or