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

Mock fails when not running test in Debug mode

5 Answers 431 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Jason Hurdlow
Top achievements
Rank 1
Jason Hurdlow asked on 20 Jan 2011, 09:21 PM
I am mocking a web-service method in my test using JustMock. The mock works as expected when I run it in debug mode. However when I run it not in debug mode, the mock fails to intercept the call and hits the actual web-service method. I'm using *Any* args in my arrange as I remember seeing something about a bug that using those fixed... 
Any ideas? Looks like a bug to me.

VS2010 Premium
JustMock (2010.3.1109.7 paid) is ON
Running from VS IDE using built-in MSTest and runner.

-Jason

5 Answers, 1 is accepted

Sort by
0
Ricky
Telerik team
answered on 24 Jan 2011, 01:45 PM
Hi Jason,

Thank you for sending the issue. In regard to the failing test when not running in debug mode, I would like to ask if it fails even if you run it separately or it fails only if you combine it with others. 

In case your tests are combined, i would recommend you to take a look at this forum post.

http://www.telerik.com/community/forums/justmock/general-discussions/mocks-failing-to-work-correctly-in-large-test-runs.aspx

Also, please check if all the supplied tests are passing as well under \%Program_Files%\Telerik\JustMock\Examples folder.  If those are failing as well, then please re-install the product.

Finally, please don’t hesitate to contact us if you still having issues.

Kind Regards,
Ricky
the Telerik team

Browse the videos here>> to help you get started with JustMock
0
Jason Hurdlow
Top achievements
Rank 1
answered on 24 Jan 2011, 06:21 PM
The tests fail (no intercept) whether they are run individually or in a group (in non-debug).
The tests all work as expected whether they are run individually or in a group (in debug mode).

All the JustMock example tests pass in both debug and non-debug form.

Any other ideas?

-Jason
0
Chris
Telerik team
answered on 25 Jan 2011, 10:49 AM
Hello Jason,

1) I guess you gave the same references both in debug and release mode but just in case could you check whether in release mode you have these two assemblies:
Telerik.JustMock.dll
Telerik.CodeWeaver.Hook.dll
in the test project's output path (e.g. bin\Release)

2) Could you check whether you have the same platform target (Any CPU / x86 / x64) both for debug and release mode?

Sorry for the inconvenience and thanks in advance for helping us identify the issue.

Greetings,
Chris
the Telerik team
Browse the videos here>> to help you get started with JustMock
0
Jason Hurdlow
Top achievements
Rank 1
answered on 25 Jan 2011, 11:12 PM
Chris-
Thanks for the reply.

Let me clarify what I mean by debug and non-debug. I'm not referring to the build configuration (Debug/Release), I'm referring to how the tests are run. I.e.:
Test -> Debug -> Tests in Current Context = Tests work
vs.
Test -> Run -> Tests in Current Context = Tests don't work (JustMock doesn't intercept call)

Both Debug and Release configurations yield the same results.

Both Telerik.JustMock.dll and Telerik.CodeWeaver.Hook.dll are present in the appropriate folders, although I did not have the CodeWeaver one in there at first (doesn't seem to make any difference if it's there or not).

All projects and configurations are set to "Any CPU". I'm running on a 64-bit build of Windows Server 2008.

Any other ideas?

I'm afraid this is going to come down to building a test project to send to you, I was just hoping to avoid having to do that.

-Jason

0
Ricky
Telerik team
answered on 26 Jan 2011, 01:21 PM
Hi Jason,

Thanks again for clarifying the issue. Doing a bit more investigation, I found that MSTest starts the development server for WCF projects and somehow intializes the client proxy. Therefore, JustMock fails to intercept the call in certain cases.

To get around this issue, i have cooked up a project which initializes the service host with mocked contract using JM MultiMock feature.

Accordingly, first I created a class that sets the context mode (to be used with ServiceHost):

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)] 
public class PartialService
{
}

Next, during the test initialization, I created the mock of PartialService that implements the target  WCF contract and added it to the service host created for the specific purpose. In this case the contract is TestService.IService1 and test initialization looks like :

[TestInitialize]  
public void TestFixtureSetUp() {
    service = Mock.Create<PartialService>(x => 
        x.Implements<IService1>()) as IService1;
    var host = new ServiceHost(service);
    host.AddServiceEndpoint(typeof(IService1), new BasicHttpBinding(), FakeUrl);  
    host.Open();  
 }
 

Here to note that the url passed in host.AddServiceEndpoint must also contain in the client section of app.config:

<system.serviceModel>
     
     <client>
          <endpoint address="http://localhost:5555/Service1.svc" binding="basicHttpBinding"
              contract="TestService.IService1" />
     </client>
</system.serviceModel>

Now, let's say we have a client project with the service reference of  TestService.IService1 and invokes a service method:

public class TheFacade
{
    public void Submit()
    {
        using (Service1Client client = new Service1Client())
        {
            client.GetData(10);
        }
    }
}

When a test method (like below) is written asserting the expected, it passes both in Debug and Run mode:

[TestMethod]
public void ShouldAssertServiceCall()
{
    bool called = false;
    Mock.Arrange(() => service.GetData(Arg.AnyInt)).DoInstead(() => called = true);
    TheFacade facade = new TheFacade();
    facade.Submit();
    Assert.IsTrue(called);
}

However, this issue occurs with MSTest only. If you run your tests using TestDriven.Net (provided that asp net development server is not running) tests will run as expected.

Finally, I am attaching the project to let you have a look and hopefully this will resolve your issue. Also, this is a generic implementation and uses only proxy therefore should work in all cases and wont's have any initialization issues at all.

Please do feel free to write us for any exceptions.


Kind regards,
Ricky
the Telerik team
Browse the videos here>> to help you get started with JustMock
Tags
General Discussions
Asked by
Jason Hurdlow
Top achievements
Rank 1
Answers by
Ricky
Telerik team
Jason Hurdlow
Top achievements
Rank 1
Chris
Telerik team
Share this question
or