views:

81

answers:

1

When I try to generate a unit test for the following method (in a public static class)

private static string[] GetFields(string line, char sep)
{
    char[] totrim = { '"', ' ' };
    return line.Split(sep).Select(col => col.Trim(totrim)).ToArray();
}

The Tests output says:

While trying to generate your tests, the following errors occurred:
This method or property cannot be called within an event handler.

It works if I make the function public - I've tried running Publicize.exe manually, it doesn't complain, but doesn't make any difference either.

A: 

If your function is private, then its private. No other assemblies, including your test framework, should be able to see it directly.

You either need to make it public (which may break your model), expose a public wrapping function that you clearly state is for testing only (which is open to abuse) or you can make it available to just your unit test component by adding the following attribute (assuming you are using C#) to the class that contains GetFields

[assembly:InternalsVisibleTo("[your unit test component name]")]

The Microsoft website has more information here

Chris Gill
Have you tested this? As far as I can see, the declaration doesn't make any difference. Tests for private methods still aren't generated. However, I can change to internal, and that works, even without the assembly declaration...
Benjol
I'm guessing your tests and in the same project/component as the code you are testing. That should make the internal option work. My code works if you have your tests in external components - this is how we currently have some tests working in one of our projects so it does work
Chris Gill