Telerik Forums
JustMock Forum
1 answer
175 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
158 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
116 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
712 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
98 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
368 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
117 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
121 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
222 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
8 answers
3.1K+ views
Hi,

I am having a problem with JustMock where tests pass when run individually but fail when all tests are run. I am not using any static or final mocking.

In the following code, the GetSectionsForAppByAppId_WithAppIdForSingleApp_ReturnsGetSectionsForAppResponse test fails.

   [TestClass]
   public class MobileAppSectionServiceTests
   {
        private const int AppId = 1;
        private const int LocalCultureId = 2;
 
        private IEnumerable<IMobileAppSection> sections;
        private GetSectionsForAppRequest request;
        private MobileAppSection section;
 
        [TestInitialize]
        public void TestInit()
        {
            section = new MobileAppSection();
            section.Id = AppId;
            section.Name = "SectionName";
            section.Summary = "Section Summary";
 
            sections = new List<IMobileAppSection> { section };
            request = new GetSectionsForAppRequest { MobileAppId = AppId, LocalCultureId = LocalCultureId};
        }
 
        [TestMethod]
        public void GetSectionsForAppByAppId_WithAppIdForSingleApp_ReturnsGetSectionsForAppResponse()
        {
            MobileAppSectionService service = new MobileAppSectionService(null);
            Mock.Arrange<IEnumerable<IMobileAppSection>>(() => service.GetSectionDomainEntitiesForAppByCulture(AppId, LocalCultureId)).Returns(sections);
            GetSectionsForAppResponse response = service.GetSectionsForAppByCulture(request);
            
            Assert.IsNotNull(response, "Response is not null");
        }
 
        [TestMethod]
        public void GetSectionsForAppByAppId_WithAppIdForSingleApp_ReturnsResponseWithSections()
        {
            IMobileAppSectionMapper _mobileAppSectionMapper = Mock.Create<IMobileAppSectionMapper>();
            MobileAppSectionService service = new MobileAppSectionService(_mobileAppSectionMapper);
            Mock.Arrange<IEnumerable<IMobileAppSection>>(() => _mobileAppSectionMapper.GetSectionsForMobileApp(AppId, LocalCultureId)).Returns(sections);
            
            GetSectionsForAppResponse response = service.GetSectionsForAppByCulture(request);
            
            Assert.IsTrue(response.MobileAppSections.Any());
        }
    }

The error message is:

Test method MobileAppDelivery.Service.Tests.Unit.MobileAppSectionServiceTests.GetSectionsForAppByAppId_WithAppIdForSingleApp_ReturnsGetSectionsForAppResponse threw exception: 
System.NullReferenceException: Object reference not set to an instance of an object. The NullReferenceException is thrown the following method of the MobileAppSectionService class.
    public List<IMobileAppSection> GetSectionDomainEntitiesForAppByCulture(int appId, int cultureId)
    {
       List<IMobileAppSection> sections = _mobileAppSectionMapper.GetSectionsForMobileApp(appId, cultureId); 
       return sections;
    }

where _mobileAppSectionMapper is null. Rewriting the test method as the following fixes the problem

        [TestMethod]
        public void GetSectionsForAppByAppId_WithAppIdForSingleApp_ReturnsGetSectionsForAppResponse()
        {
            IMobileAppSectionMapper mapper = Mock.Create<IMobileAppSectionMapper>();
 
            MobileAppSectionService service = new MobileAppSectionService(mapper);
            Mock.Arrange<IEnumerable<IMobileAppSection>>(() => service.GetSectionDomainEntitiesForAppByCulture(AppId, LocalCultureId)).DoNothing().Returns(sections);
            GetSectionsForAppResponse response = service.GetSectionsForAppByCulture(request);
            
            Assert.IsNotNull(response, "Response is not null");
        }

So why does the original test only pass when run individually? 
My instincts tell me that the original test was not written correctly but it still passed. 

Thanks!
Ricky
Telerik team
 answered on 24 Feb 2012
Narrow your results
Selected tags
Tags
+? more
Top users last month
Anislav
Top achievements
Rank 6
Silver
Bronze
Bronze
Jianxian
Top achievements
Rank 1
Iron
Marco
Top achievements
Rank 3
Iron
Iron
Iron
Jim
Top achievements
Rank 2
Iron
Iron
Nurik
Top achievements
Rank 2
Iron
Iron
Want to show your ninja superpower to fellow developers?
Top users last month
Anislav
Top achievements
Rank 6
Silver
Bronze
Bronze
Jianxian
Top achievements
Rank 1
Iron
Marco
Top achievements
Rank 3
Iron
Iron
Iron
Jim
Top achievements
Rank 2
Iron
Iron
Nurik
Top achievements
Rank 2
Iron
Iron
Want to show your ninja superpower to fellow developers?
Want to show your ninja superpower to fellow developers?