I'm having trouble when mocking methods from VirtualPathUtility class, i.e.VirtualPathUtility.ToAbsolute(...)
Here is a test sample :
public class XTest
Mock.Replace(() => System.Web.VirtualPathUtility.ToAbsolute(Arg.AnyString)).In<XTest>(x => x.Test());
public void Test()
Mock.Arrange(() => System.Web.VirtualPathUtility.ToAbsolute(Arg.AnyString)).Returns("absolute");
string r = System.Web.VirtualPathUtility.ToAbsolute("test");
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=18.104.22.168, Culture=neutral, PublicKeyToken=87210992829a189d' or one of its dependencies. The system cannot find the file specified.
Any idea what am I doing wrong?
5 Answers, 1 is accepted
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:
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:
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.
the Telerik team
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=22.214.171.124, Culture=neutral, PublicKeyToken=87210992829a189d' or one of its dependencies. The system cannot find the file specified.
at System.Web.VirtualPathUtility.ToAbsolute(String virtualPath)
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.
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:
Hope this helps.
the Telerik team
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.
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.
the Telerik team