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

Mocking VirtualPathUtility

5 Answers 237 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Heiko
Top achievements
Rank 1
Heiko asked on 26 Oct 2012, 12:25 AM
Hi,
I'm having trouble when mocking methods from VirtualPathUtility class, i.e.VirtualPathUtility.ToAbsolute(...)

Here is a test sample :

    public class XTest
    {
        static XTest()
        {
            Mock.Replace(() => System.Web.VirtualPathUtility.ToAbsolute(Arg.AnyString)).In<XTest>(x => x.Test());
        }

        [Fact]
        public void Test()
        {
            Mock.SetupStatic(typeof(System.Web.VirtualPathUtility));
            Mock.Arrange(() => System.Web.VirtualPathUtility.ToAbsolute(Arg.AnyString)).Returns("absolute");

            string r = System.Web.VirtualPathUtility.ToAbsolute("test");

            Assert.Equal("absolute", r);
        }
    }

I've also tried using Mock.Initialize, Mock.Replace is static constructor but didn't help.
I'm always getting exception:

System.IO.FileNotFoundException : Could not load file or assembly 'Telerik.CodeWeaver.Api, Version=1.0.0.0, Culture=neutral, PublicKeyToken=87210992829a189d' or one of its dependencies. The system cannot find the file specified.

Any idea what am I doing wrong?

Thanks

Heiko

5 Answers, 1 is accepted

Sort by
0
Kaloyan
Telerik team
answered on 29 Oct 2012, 03:22 PM
Hi Heiko,

 Thank you for contacting Telerik support.

 In order to mock "VirtualPathUtility" class, you do not need to use Mock.Replace, as it is generally used for mocking MSCorlib members. You can read more about it here:
 - http://www.telerik.com/help/justmock/advanced-usage-mscorlib-mocking.html

 To mock this class, I simply dropped the XTest() method from your example and the rest behaved as expected. I added some more content to your test method just to show that it is working for other methods from the "VirtualPathUtility" class. Here is the example:
[TestMethod]
public void ShouldArrangeMethodsFromVirtualPathUtilityClass()
{
    Mock.SetupStatic(typeof(System.Web.VirtualPathUtility), Behavior.CallOriginal, StaticConstructor.Mocked);
 
    string expected = "Telerik";
 
    // Arrange
    Mock.Arrange(() => System.Web.VirtualPathUtility.ToAbsolute(Arg.AnyString)).Returns(expected);
    Mock.Arrange(() => System.Web.VirtualPathUtility.IsAbsolute(Arg.AnyString)).Returns(true);
 
    // Act
    string actual = System.Web.VirtualPathUtility.ToAbsolute("test");
    bool actualBool = System.Web.VirtualPathUtility.IsAbsolute("test");
 
    // Assert
    Assert.AreEqual(expected, actual);
    Assert.IsTrue(actualBool);
}

 If this does not solve your issue and the exception still remain, I would recommend you to update the JustMock references in your project.

 I hope this helps. If there is anything else, I can help you with, please do not hesitate to ask.

Greetings,
Kaloyan
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Heiko
Top achievements
Rank 1
answered on 30 Oct 2012, 12:15 AM
Hi,
I've tried the sample you provided and it fails with the same exception:

Result Message:    System.IO.FileNotFoundException : Could not load file or assembly 'Telerik.CodeWeaver.Api, Version=1.0.0.0, Culture=neutral, PublicKeyToken=87210992829a189d' or one of its dependencies. The system cannot find the file specified.
Result StackTrace:    
at System.Web.VirtualPathUtility.ToAbsolute(String virtualPath)
   at ShouldArrangeMethodsFromVirtualPathUtilityClass()

I'm running it under xUnit, could that be the reason? That's the only difference I can think of. And when I try to debug it, then it's successful, strange.

Thanks

Heiko
0
Ricky
Telerik team
answered on 01 Nov 2012, 09:27 PM
Hi Heiko,

I just wrote your test using XUnit and it is working as expected. I am also sending it to you so please let me know if that works at your end as well.

Moreover, make sure that Telerik.JustMock.DLL and Telerik.CodeWeaver.Api.DLL is in same folder as it is loaded dynamically by JustMock DLL. Further I am using TestDriven.Net and you do not need Mock.Replace for mocking members from System.Web.DLL

In addition, you generally don't need to do Mock.SetupStatic unless you’re doing strict mocking or want the type to behave like stub.

Therefore, my final test method looks like:


public class XTest
{
    [Fact]
    public void Test()
    {
        Mock.Arrange(() => System.Web.VirtualPathUtility.ToAbsolute(Arg.AnyString)).Returns("absolute");
 
        string r = System.Web.VirtualPathUtility.ToAbsolute("test");
 
        Assert.Equal("absolute", r);
    }
}


Hope this helps.

Kind Regards
Mehfuz
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Heiko
Top achievements
Rank 1
answered on 08 Nov 2012, 12:00 PM
Hi,
I've tried your solution. And I have the same issue when I use the VS 2012 build in test runner (vstest.executionengine.x86.exe).
It's independent whether I use xunit or nunit, the result is the same.
When I use different test runner, for example from JustCode, then it works.

Heiko
0
Ricky
Telerik team
answered on 12 Nov 2012, 11:29 PM
Hi Heiko,

I just installed xUnit.net runner for VS 2012 and was able to run the test inside VS Unit Test Explorer while debugging it.



I am using Visual Studio 2012 RTM and referenced JustMock from installation directory.

However, I was able to get your exception in run mode. This is because the out directory does not contain Telerik.CodeWeaver.Api.DLL that is internally loaded by JustMock assembly. Changing the target framework to .net 3.5 from properties page solved the issue (which is strange). This behavior is not visible mocking with MSTest.

We will keep investing the issue and keep you posted on anything that we find in that regard. In the meantime, please check the workaround or use other test runners like TestDriven.Net or JustCode which do not copy DLLs to a separate folder.

Kind Regards
Ricky
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
General Discussions
Asked by
Heiko
Top achievements
Rank 1
Answers by
Kaloyan
Telerik team
Heiko
Top achievements
Rank 1
Ricky
Telerik team
Share this question
or