.

nunit template for VS2010

here is the template for nunit (from: )

download: TestClassLibrary or a test example project

copy to : C:\Users\pete\Documents\Visual Studio 2010\My Exported Templates

all i did was export it from file-export template – and this is what was rendered

it’s in visual c# > called: testclasslibrary

 

simplified:

[TestFixture]
public class Class1
{
    [Test]
    public void test1()
    {
        Assert.True(true);
    }
}

Unit test an Exception

Testing an exception as follow:

[Test]
public void TestCorrectErrorTypeAndMessage()
{
    var ex = Assert.Throws<Exception>(
                    , () =>
                        {
                            DoThis();
                        });
    Assert.AreEqual("error123", ex.Message);
}

public void DoThis()
{
    throw new Exception("error123");
}

Another real example:

[Test]
public void MustHaveMetricsBeforeTakeSnapshot()
{
    var snapshotTaker = new SnapshotTaker();
        snapshotTaker.DataPoints = dataPoints;
        var ex = Assert.Throws<Exception>(() =>
                {
                    snapshotTaker.TakeSnapshot();
                });
    Assert.IsTrue(ex.Message.Contains("must provide"), "incorrect error message");
}

Unit test protected methods using Rhino mocks

If your method is in a different assembly then you need to allow access to your own assembly (e.g MyAssembly) and e.g. Rhino mocks () or whatever you use for mocking

you can do so:

[assembly: InternalsVisibleTo("MyAssembly")]
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")]

then your unit test would look something like this:

[TestMethod]
public void TestMethod1()
{
    var txt = "testdata";
    var rawRequestProcessor = MockRepository.GenerateStub<RawRequestProcessor>();
    rawRequestProcessor.Process(txt);
    Assert.AreEqual(txt, rawRequestProcessor.RawInput);
    rawRequestProcessor.Expect(m => m.processInput());
    rawRequestProcessor.VerifyAllExpectations();
}

example of the code being tested here:

public class RawRequestProcessor
{
    public void Process(string txt)
    {
        RawInput = txt;
    }

    private string _rawInput;
    public string RawInput {
        get { return _rawInput; }
        set
        {
            _rawInput = value;
            processInput();
        }
    }

    internal virtual void processInput()
    {
            
    }
}

 

 

Unit Testing Cross Assemblies with “internal” declarations

The steps to allow access to external assemblies mainly for testing purposes:

  1. Navigate to your tests assembly / project.
  2. double click on the properties folder.
  3. copy the full assembly name - e.g. Bitlink.Chess.OpeningExplorer.Tests
  4. go to your assembly that you want to test (the one which uses internal declarations)
    in my case: Bitlink.Chess.OpeningExplorer.Library
  5. expand the properties folder, locate and open AssemblyInfo.cs
  6. at the very bottom of this file, put a reference to your test assembly, e.g.:
    [assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Bitlink.Chess.OpeningExplorer.Tests")]

if you have signed your assemblies, you would need a further signature:

[assembly: InternalsVisibleTo("...Tests")] , PublicKey=……

for that you could extract the public key token of your keyfile over two steps

  1. extract the public key from the keyfile with
    sn –p <keyfile.snk> <keyfile.snk.PublicKey>
  2. show the public key token with
    sn -t <keyfile.snk.PublicKey>

or use the GetPublicKey utility that’s going around:  download GetPublicKey

[Resolved!] System.BadImageFormatException: The format of the file ‘xxx’ is invalid

I got the following error during unit testing.
System.BadImageFormatException: The format of the file ‘xxx’ is invalid

reason was that the project I tested was X86 but my testing application was “Any CPU”

Solution:
ensure that all your projects compile to the same platform, for example:
double click on your “properties” folder
set all individual platforms targets (of all your projects) to e.g. x86

that should resolve it.

– any other issues / solutions, please post below!