I love LINQPad and use it daily. I have been trying to find a way to create and run ad-hoc tests with nunit and TypeMock in LINQPad for a while.

So I stumbled around and got some results, but some links are still missing.

Here's what I have done:

  1. Create a new query in linqpad, add reference to NUnit and Typemock assemblies.

  2. Create an Nunit runner. (Thanks to http://www.paraesthesia.com/archive/2008/02/21/template-for-quick-typemock-testing.aspx)

  3. Add a couple of environment variables to enable Typemock profiler.

At this point, I am able to get Nunit and Typemock to work with some manual step (need to copy nunit and typemock dlls to the executing directory, e.g., \AppData\Local\Temp\1\LINQPad\skbidgcw).

But if I add our assemblies (which I want to test) to the LINQPad script, the test would fail, due to NUnit unable to find the assemblies in the executing directory. I even tried copying all the DLLs to that, but this would also fail due to:

System.IO.FileNotFoundException : Could not load file or assembly 'LINQPad, Version=1.0.0.0, Culture=neutral, PublicKeyToken=21353812cd2a2db5' or one of its dependencies. The system cannot find the file specified.

My linqpad query is here: http://pastebin.com/QtPNCv25

Any help will really be appreciated!

As a side note, I also tried using NUnitLite, while it runs Nunit tests beautifully, I can't find a way to make it work with Typemock, it throws error saying "Typemock Isolator needs to be linked with Coverage Tool to run".

有帮助吗?

解决方案

Finally found a way to do what I wanted using NUnitLite.

  • Added the following environment variables to my system in order to enable Typemock profiler.

Cor_Enable_Profiling=0×1

COR_PROFILER={B146457E-9AED-4624-B1E5-968D274416EC}

(NUnitLite may have been running the test in another AppDomain, and so setting the environment variables through LINQPad did not enable the profiler; this may be circumvented if there's a switch to do so in NUnitLite, but I haven't researched.)

  • Put TypeMock.dll and Typemock.ArrangeActAssert.dll in Linqpad's plugins folder.

  • Create new query and add NUnitLite through Nuget. Add necessary namespaces.

Now the following should work.

void Main()
{
    new NUnitLite.Runner.TextUI().Execute( new[]{"-noheader"} );
}

// Define other methods and classes here
[Test, Isolated]
public void TestMock()
{
    Isolate.WhenCalled( () => DateTime.Now ).WillReturn( DateTime.Today );

    var dt = DateTime.Now;

    Assert.AreEqual( DateTime.Today, dt );
}

Now my life should be considerably easier. Thanks!

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top