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

Mocking Extention Method fails with"QTAgendt32 ahs stopped working"

5 Answers 35 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Masoud
Top achievements
Rank 1
Masoud asked on 30 Apr 2013, 05:09 PM
Hi,

I am working on an MVC 4 project againt .net 4.5 on Visual Studio 2012.  JustMock works great until I try to mock an extention method.
Here ist what I did:
- Installed JustMock from the msi file
- In Visual StudioTelerik\JustMock doesn't offer the menu "Enable JustMock"/"Disable JustMock" but "Enable Profiler" and "Disable Profiler".  Enabled is chosen.  (http://www.telerik.com/help/justmock/advanced-usage.html)
- Callling options I can select dot.cover (product from JetBrains)
I mock an extention method as recommended in the documentation (http://www.telerik.com/help/justmock/advanced-usage-extension-methods-mocking.html)

Mock.Arrange(() => projekte.WithPath(Arg.IsAny<IPrefetchPath2>())).Returns(projekte).MustBeCalled();

WithPath is the extention method.

The test fails
- first I get a message box that says "vstest.executionengine.exe has stopped working"
- i have to close the messagebox
- in the test explorer the following message is given: 

System.InvalidOperationException: There is no method 'WithPath' on type 'SupportClasses.QueryableExtensionMethods' that matches the specified arguments

Thank you for your support on this issue!


5 Answers, 1 is accepted

Sort by
0
Kaloyan
Telerik team
answered on 01 May 2013, 08:45 AM
Hi Klaus,

Thank you for the detailed message.

In one of the latest JustMock official releases, we changed the Enable/Disable JustMock with the Enable/Disable Profiler option. However, it has the same functionality but a more suitable name, as it enables or disables the JustMock profiler. However, thank you for noticing that our documentation needs update. We have granted you some Telerik points for this.

In JustMock Options, you will find a configuration window which will allow you to integrate JustMock with other 3rd party profiler using tools (like dotCover, as you have noticed). More about this could be found here.

About the vstest.executionengine.exe error you are experiencing, it is a bug in JustMock which we have already fixed and this fix will be included in the next official release. A workaround I can suggest is to uncheck the "Keep test execution engine running between test runs" from Tools -> Options -> Web Performance Test Tools (I have attached a screenshot to guide you further).

Finally, as such behavior of JustMock is not previously reported, is it possible to wrap the failing test method along with the SUT (System Under Test) into a compiling project and send it with your next message? Having it, I will be able to investigate it further and provide solution or at least a workaround until the issue is fixed.

I hope this helps.

Kind regards,
Kaloyan
the Telerik team
Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
0
Masoud
Top achievements
Rank 1
answered on 06 May 2013, 03:34 PM
Hi Kaloyan,

thank you for your reply.

First here is the configuration I am using:
- Windows Web Server 2008 R2, running in VMWare Workstation
- Visual Studio 2012 Update 2
- Resharper (latest version)
- Dot.Cover (latest version)
- JustMock (latest version, Profiler enabled, no external Profiler is linked)

The project is an MVC 4 projekt in c# running against dot.net 4.5.
Telerik.JustMock version is ereferenced by the test projekt.

Here an example of the calls that are failing

var queryable = new List<ProjektEntity> { projekt }.AsQueryable();
Mock.Arrange(() => queryable.WithPath(Arg.IsAny<IPrefetchPath2>())).Returns(queryable).MustBeCalled();


I changed the setting you described (unchecking "Keep test execution engine running between test runs") and I am no longer getting the error with QTAgendt32.  The error I am getting right now is the following:



System.InvalidOperationException: There is no method 'WithPath' on type 'SD.LLBLGen.Pro.LinqSupportClasses.QueryableExtensionMethods' that matches the specified arguments
Result StackTrace:
at System.Linq.EnumerableRewriter.FindMethod(Type type, String name, ReadOnlyCollection`1 args, Type[] typeArgs, BindingFlags flags)
   at System.Linq.EnumerableRewriter.VisitMethodCall(MethodCallExpression m)
   at System.Linq.Expressions.OldExpressionVisitor.Visit(Expression exp)
   at System.Linq.Expressions.OldExpressionVisitor.VisitExpressionList(ReadOnlyCollection`1 original)
   at System.Linq.EnumerableRewriter.VisitMethodCall(MethodCallExpression m)
   at System.Linq.Expressions.OldExpressionVisitor.Visit(Expression exp)
   at System.Linq.EnumerableExecutor`1.Execute()
   at System.Linq.EnumerableQuery`1.System.Linq.IQueryProvider.Execute[S](Expression expression)
   at System.Linq.Queryable.Single[TSource](IQueryable`1 source, Expression`1 predicate)

WithPath is the extention method I am attempting to mock.  
The error appears to happen because the static method is not being mocked at all....
The librariy containing the extention method is referenced and in the bin folder of the test application.
I am getting the same error when I use the VS 2012 test runner and when using ReSharper's test runner.



Thank you again for your help!

0
Kaloyan
Telerik team
answered on 07 May 2013, 01:50 PM
Hi Klaus,

In order to arrange the return of a collection, I would suggest the using of ReturnsCollection() instead of Returns(). I made a sample test, mocking an extension method and it works as expected:
[TestMethod]
public void TestMethod1()
{
    // Arrange
    var queryable = new List<string> { "Telerik" }.AsQueryable();
    Mock.Arrange(() => queryable.WithPath<string>(Arg.AnyString)).ReturnsCollection(queryable).MustBeCalled();
     
    // Act
    var actual = queryable.WithPath<string>("asd"); // Acts by calling the WithPath method
 
    // Assert
    Assert.AreEqual(actual.First(), queryable.First()); // Asserts the collections are equal.
    Mock.Assert(queryable); // Asserts if WithPath has been called.
}

public static class MyExtensions
{
    public static List<TSource> WithPath<TSource>(this IQueryable list, TSource item)
    {
        throw new NotImplementedException();
    }
}

I hope this helps. Please, contact us again if you need further assistance.

Kind regards,
Kaloyan
the Telerik team
Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
0
Masoud
Top achievements
Rank 1
answered on 07 May 2013, 04:39 PM
Hi Kaloyan,

thank you for your suggestion to use the ReturnCollection.

I now am getting a stack overflow excpetion error running the test using the VS Test Runner.  Following is the Call Stack I was able to obtain:

  [Native to Managed Transition]
> Microsoft.VisualStudio.QualityTools.Tips.UnitTest.Adapter.dll!Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestExecuter.DefaultTestMethodInvoke(object[] args) + 0x43 bytes
  Microsoft.VisualStudio.QualityTools.Tips.UnitTest.Adapter.dll!Microsoft.VisualStudio.TestTools.TestTypes.Unit.DefaultTestMethodDecorator.Invoke(object[] args) + 0xb bytes
  Microsoft.VisualStudio.QualityTools.Tips.UnitTest.Adapter.dll!Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestExecuter.RunTestMethod() + 0x14b bytes
  Microsoft.VisualStudio.QualityTools.Tips.UnitTest.Adapter.dll!Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestExecuter.ExecuteTest() + 0xeb bytes
  Microsoft.VisualStudio.QualityTools.Tips.UnitTest.Adapter.dll!Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestExecuter.Execute(Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestResult result) + 0x95 bytes
  Microsoft.VisualStudio.QualityTools.Tips.UnitTest.Adapter.dll!Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestRunner.ExecuteSingleTest(Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestExecuter executer, Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestResult result, Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement test, Microsoft.VisualStudio.TestTools.Execution.ITestContext testContext, Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestAdapterContext userContext, bool isLoadTest) + 0xe bytes
  Microsoft.VisualStudio.QualityTools.Tips.UnitTest.Adapter.dll!Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestRunner.ExecuteSingleTest(Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestExecuter executer, Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement test, Microsoft.VisualStudio.TestTools.Execution.ITestContext testContext, Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestAdapterContext userContext, bool isLoadTest) + 0x68 bytes
  Microsoft.VisualStudio.QualityTools.Tips.UnitTest.Adapter.dll!Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestRunner.Run(Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement test, Microsoft.VisualStudio.TestTools.Execution.ITestContext testContext, bool isLoadTest, bool useMultipleCpus) + 0x292 bytes
  [Appdomain Transition]
  Microsoft.VisualStudio.QualityTools.Tips.UnitTest.Adapter.dll!Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestAdapter.Run(Microsoft.VisualStudio.TestTools.Common.ITestElement testElement, Microsoft.VisualStudio.TestTools.Execution.ITestContext testContext, bool isLoadTest) + 0x37a bytes
  Microsoft.VisualStudio.QualityTools.Tips.UnitTest.Adapter.dll!Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestAdapter.Run(Microsoft.VisualStudio.TestTools.Common.ITestElement testElement, Microsoft.VisualStudio.TestTools.Execution.ITestContext testContext) + 0x2a bytes
 

I managed however to make the tests run.  What I did was create two instances of IQueryable<>.  One was used to invoke the extention method, the other one was returned as result.  An example:
var queryable0 = new List<ProjektEntity> { projekt0, projekt1, projekt2 }.AsQueryable();
var queryable1 = new List<ProjektEntity> { projekt0, projekt1, projekt2 }.AsQueryable();
Mock.Arrange(() => Db.GetProjekt()).Returns(queryable0).MustBeCalled();
Mock.Arrange(() => queryable0.WithPath(Arg.IsAny<IPrefetchPath2>())).ReturnsCollection(queryable1).MustBeCalled();

I have two instances queryable0 and queryable1.  This works!!!  Also, it works with ReturnsCollection and Returns.

again, thank you for your effort and for a great product!


0
Kaloyan
Telerik team
answered on 08 May 2013, 07:02 AM
Hi Klaus,

I am glad the issue is solved now. Also, thank you for sharing your further efforts.

Do not hesitate to contact us again if you need help.

Regards,
Kaloyan
the Telerik team
Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
Tags
General Discussions
Asked by
Masoud
Top achievements
Rank 1
Answers by
Kaloyan
Telerik team
Masoud
Top achievements
Rank 1
Share this question
or