Telerik Forums
JustMock Forum
4 answers
798 views
Hi.

I'm performing an evaluation of JustMock and running through some simple cases. I'm finding the manual a bit light on examples and quickly running into cases which fail for reasons which I can't find explained anywhere.

    class Fake1
    {
        public Fake1(int a) { Prop1 = a; }
        public int Prop1 { get; set; }
    }
 
    [Test]
    public void Test1()
    {
        Fake1 f = Mock.Create<Fake1>(7);
//      Mock.ArrangeSet(() => f.Prop1 = Arg.AnyInt).CallOriginal(); // FAILS
        Mock.Arrange(() => f.Prop1).CallOriginal();
        f.Prop1 = 5;
        int a = f.Prop1;
        Assert.AreEqual(5, a);
    }
 
    [Test]
    public void Test2()
    {
//      Fake1 f = Mock.Create<Fake1>(7); // FAILS
        Fake1 f = Mock.Create<Fake1>(Constructor.Mocked);
        int intercepted = 0;
        Mock.ArrangeSet(() => f.Prop1 = Arg.AnyInt).DoInstead((int x) => intercepted = x);
        f.Prop1 = 5;
        Assert.AreEqual(5, intercepted);
    }

How come in Test1 I have to arrange the property get but not the property set? It isn't a problem, just rather unintuitive and I don't see anywhere where this behaviour is explained. If I uncomment the commented out line it fails with:

Test 'M:Library1.Tests.Test3' failed: There were some problems intercepting the mock call. Optionally, please make sure that you have turned on JustMock's profiler while mocking concrete members.
    Telerik.JustMock.MockException: There were some problems intercepting the mock call. Optionally, please make sure that you have turned on JustMock's profiler while mocking concrete members.
    at Telerik.JustMock.Expectations.Expectation.ThrowForInvalidCall(IInvocation invocation)
    at Telerik.JustMock.Expectations.Expectation.Process(IInvocation invocation)
    at Telerik.JustMock.Mock.ArrangeSet(Action action)
    Class1.cs(84,0): at Library1.Tests.Test3()

Why does Test2 fail if I don't mock the constructor? This might be a problem for me. This test is just noddy code but in real code one of my most desirable use cases is to be able to construct an object with various arguments and then mock some of its properties/methods while other properties/methods call the original implementation. If I uncomment the commented out line it fails with the same error as Test1.

I'm using JustMock 2012 SP1, assembly version 2012.1.229.0, 60 day trial version

Thanks.
Ricky
Telerik team
 answered on 28 Mar 2012
1 answer
183 views
hi

is it possible to use   ReturnsCollection   with  Mock.NonPublic.Arrange

Mock.NonPublic.Arrange(fw, "GetRoleClassesAndRights", 10).ReturnsCollection(CreateDocumentClassAndRoleRightsList());

I could not  do it.. Why.. there comes error ..ReturnsCollection    

Error 122 'Telerik.JustMock.Expectations.ActionExpectation' does not contain a definition for 'ReturnsCollection' and no extension method 'ReturnsCollection' accepting a first argument of type 'Telerik.JustMock.Expectations.ActionExpectation' could be found (are you missing a using directive or an assembly reference?)


Ricky
Telerik team
 answered on 27 Mar 2012
3 answers
165 views
I was putting a small example program together and noticed that JustMock doesn't work with the .NET Framework 4 Client Profile.  Admittedly, this isn't a real-world scenario, so I don't know how important it is, but thought I would mention it anyway.  Here's the message:

Warning 1 The referenced assembly "Telerik.JustMock" could not be resolved because it has a dependency on "System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" which is not in the currently targeted framework ".NETFramework,Version=v4.0,Profile=Client". Please remove references to assemblies not in the targeted framework or consider retargeting your project. MockTest

A second observation from reading the message above; I'm not sure that there should be a dependency on System.Web.  i.e. What if I wasn't building a Web application.

It's just a console app and I had the code in the Main method like this:

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using Telerik.JustMock;  
 
namespace MockTest  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            ICustomer cust = Mock.Create<ICustomer>();  
 
            Order ord = new Order(cust);  
        }  
    }  
}  
 

BTW, I already know how to create a test project and so on, but was just fiddling around and thought this info might be interesting for you.

Joe
Ricky
Telerik team
 answered on 26 Mar 2012
1 answer
126 views
Hello together

I'm just trying to mock the SPContentTypeCollection class out of the SharePoint 2010 assembly but I'm facing a problem arranging a mock for the "BestMatch" method. I've already searched for a solution but cannot find any information about it.

This is my testcode:

SPContentTypeCollection mockedContentTypeCollection = Mock.Create<SPContentTypeCollection>();
Mock.Arrange(() => mockedList.ContentTypes).Returns(mockedContentTypeCollection);
 
SPContentTypeId mockedContentTypeId = Mock.Create<SPContentTypeId>();
Mock.Arrange(() => mockedContentTypeCollection.BestMatch(Arg.IsAny<SPContentTypeId>())).Returns(mockedContentTypeId).MustBeCalled();

The first three lines work perfectly, but an error is thrown for the fourth line. The mockedList is being creatd within a "TestInitialize" method.

This ist the call stack for the error occuring:

Test method XX.XX.Data.Tests.Repositories.XXRepositoryTests.AddPaymentSucceedsTest threw exception:
System.NullReferenceException: Object reference not set to an instance of an object.
at Telerik.JustMock.Weaver.Interceptors.WeaverInterceptor.GetRuntimeMethod(IInvocation invocation)
at Telerik.JustMock.Weaver.Interceptors.WeaverInterceptor.Telerik.JustMock.Weaver.Interceptors.Abstraction.IWeaverInterceptor.Intercept(IInvocation invocation)
at SPContentTypeId_Interceptor_d2e70383884d4efca5b3ddb76451ab0c.Intercept(Object, SPContentTypeId, ref Boolean)
at Microsoft.SharePoint.SPContentTypeId.Equals(Object o)
at Telerik.JustMock.Utility.IsDefault(Type valueType, Object value)
at Telerik.JustMock.Setup.MethodCall.GetNewInstance(Object[] arguments, Type[] argumentTypes)
at Telerik.JustMock.Interceptors.MockInterceptor.CreateMethodInstance(Object[] userArgs, Type[] types)
at Telerik.JustMock.Interceptors.MockInterceptor.CreateMethodInstance(Object[] userArgs, Type[] types, Instruction instruction)
at Telerik.JustMock.Interceptors.MockInterceptor.Intercept(MockInvocation invocation)
at Telerik.JustMock.Weaver.Interceptors.WeaverInterceptor.Telerik.JustMock.Weaver.Interceptors.Abstraction.IWeaverInterceptor.Intercept(IInvocation invocation)
at Telerik.JustMock.Handlers.WeaverInterceptorHandler.Invoke(Object[] args)
at Telerik.JustMock.MockContext`1.SetupMock(MockExpression`1 expression)
at Telerik.JustMock.MockContext`1.SetupMock(Expression`1 expression)
at Telerik.JustMock.Mock.<>c__DisplayClass1`1.<Arrange>b__0(MockContext`1 x)
at Telerik.JustMock.MockContext.Setup(Instruction instruction, Func`2 function)
at Telerik.JustMock.Mock.Arrange(Expression`1 expression)
at XX.XX.Data.Tests.Repositories.XXRepositoryTests.AddPaymentSucceedsTest() in XXRepositoryTests.cs: line 163

Has someone face the same problem already and / or can someone help me finding a solution?

Thanks
Andreas
Ricky
Telerik team
 answered on 22 Mar 2012
22 answers
741 views
Hi

I've just got up and running with JustMock to perform tests that need to isolate Microsoft's Exchange Services API, which contains many sealed and internal classes, and so requires the elevated mode in Visual Studio to enable the profiler.  

Right now these tests are failing my Continuous Integration build on TeamCity because I need to enable the profiler.

Please can you advise on how to get this up and running.

Stack trace is as follows:

Test(s) failed. Telerik.JustMock.MockException : Could not create mock from sealed class when profiler is not enabled.

   at Telerik.JustMock.MockManager.CreateInstance(Container container)

   at Telerik.JustMock.MockManager.SetupMock(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]()


Thanks,

Chris

FWIW: I'm currently running a private build of JustMock from last week that fixes an issue with covariance and contravariance (see http://www.telerik.com/account/support-tickets/view-ticket.aspx?threadid=457316)
Ricky
Telerik team
 answered on 21 Mar 2012
3 answers
101 views
Is it at all possible to get the commercial functionality of the MockInitializer parts to work with the Silverlight library or is it forever left with only the free functionality? (which does not include the MockInitializer)
Ricky
Telerik team
 answered on 12 Mar 2012
1 answer
381 views
If you've got tests that use JustMock, do you need to install it on the TFS build server for them to run? Or will it pull in the right DLL references automatically? My manager is looking for a good mocking solution without that setup overhead.
Ricky
Telerik team
 answered on 05 Mar 2012
5 answers
122 views
Hi,

We have some unit tests that we are trying to convert from MSTest to nUnit. When we run the unit tests (using Resharper) in MSTest they run without any exceptions, but when we try to run it in NUnit they fail.

The code we are running is:
var httpResponse = new HttpResponse(null);
var httpRequest =
new HttpRequest(string.Empty, "http://somewhere.com/", null);
var currentContext = Mock.Create<HttpContext>(() =>
new HttpContext(httpRequest, httpResponse));
Mock.Arrange(() => HttpContext.Current).Returns(currentContext);
Assert.IsNotNull(HttpContext.Current);
Assert.IsNotNull(HttpContext.Current.Request);<br>

Using nUnit it fails on the first assertion.

Any ideas why this is happening?


Craig
Ricky
Telerik team
 answered on 05 Mar 2012
5 answers
124 views
Following the directions here: http://www.telerik.com/help/justmock/advanced-usage-future-mocking.html 

I was able to successfully Future Mock classes that I created, but I can't seem to get Future Mocking working with TfsTeamProjectCollection.

Here are two tests that are failing:

[TestMethod]
public void TfsTeamProjectCollection_Create_returns_VersionControlServer()
{
    // Arrange
    var tfsTeamProjectCollection = Mock.Create<TfsTeamProjectCollection>(Constructor.Mocked);
    var versionControlServer = Mock.Create<VersionControlServer>();
 
    Mock.Arrange(() => tfsTeamProjectCollection.GetService<VersionControlServer>())
        .Returns(versionControlServer)
        .IgnoreInstance();
 
    var newTfsTeamProjectCollection = new TfsTeamProjectCollection(new Uri("about:blank"));
 
    // Act
    var result = newTfsTeamProjectCollection.GetService<VersionControlServer>();
 
    // Assert
    Assert.AreEqual(versionControlServer, result);
}
 
[TestMethod]
public void TfsTeamProjectCollection_Create_throws_NotImplementedException()
{
    // Arrange
    var tfsTeamProjectCollection = Mock.Create<TfsTeamProjectCollection>(Constructor.Mocked);
    var versionControlServer = Mock.Create<VersionControlServer>();
 
    Mock.Arrange(() => tfsTeamProjectCollection.GetService<VersionControlServer>())
        .Throws(new NotImplementedException())
        .IgnoreInstance();
 
    var newTfsTeamProjectCollection = new TfsTeamProjectCollection(new Uri("about:blank"));
 
    try
    {
        // Act
        newTfsTeamProjectCollection.GetService<VersionControlServer>();
 
        // Assert
        Assert.Fail("An Exception was expected.");
    }
    catch (NotImplementedException)
    {
    }

Here's a link to a sample project: http://dl.dropbox.com/u/1006254/JustMockTestProject5.zip

Ricky
Telerik team
 answered on 29 Feb 2012
1 answer
229 views
Is there a way to mock the increment/decrement operators?

Example:

MyEventHandler += HandlerToAdd;
MyEventHandler -= HandlerToSubtrack;

It seems the typical property mocking doesn't work here.
Ricky
Telerik team
 answered on 27 Feb 2012
Narrow your results
Selected tags
Tags
+? more
Top users last month
Rob
Top achievements
Rank 3
Iron
Iron
Iron
Atul
Top achievements
Rank 1
Iron
Iron
Iron
Alexander
Top achievements
Rank 1
Veteran
Iron
Serkan
Top achievements
Rank 1
Iron
Shawn
Top achievements
Rank 1
Iron
Iron
Want to show your ninja superpower to fellow developers?
Top users last month
Rob
Top achievements
Rank 3
Iron
Iron
Iron
Atul
Top achievements
Rank 1
Iron
Iron
Iron
Alexander
Top achievements
Rank 1
Veteran
Iron
Serkan
Top achievements
Rank 1
Iron
Shawn
Top achievements
Rank 1
Iron
Iron
Want to show your ninja superpower to fellow developers?
Want to show your ninja superpower to fellow developers?