Archives for: April 2011

04/20/11

Permalink 08:08:08 pm, by truewill Email , 165 words, 1190 views   English (US)
Categories: Tips, Testing, C#, IoC, .NET, Tools

Moq v4 functional specifications

The Moq mocking library only gets better. Here’s a LINQPad example of the functional specifications introduced in v4.

void Main()
{
    var foo = Mock.Of<IFoo>(f =>
        f.Id == 1 &&
        f.Who == "me" &&
        f.GetBar(It.IsAny<string>()) == Mock.Of<IBar>(
            b => b.Name == "Fred"));

    Console.WriteLine(foo.Id);
    Console.WriteLine(foo.Who);
    Console.WriteLine(foo.GetBar("whatever").Name);

    // If you need verification and still want to use
    // the functional style:
    Mock.Get(foo).Verify(f => f.GetBar("whatever"));
}

public interface IFoo
{
    int Id { get; }
    string Who { get; }
    IBar GetBar(string name);
}

public interface IBar
{
    string Name { get; }
}

This prints:

1
me
Fred

No more having to type "mock.Object"!

For more details, see Old style imperative mocks vs moq functional specifications and this Moq Discussions thread. Note that Daniel Cazzulino (kzu) suggests separating behavior-verification mocks and stubs; this syntax is most useful for stubbing.

04/13/11

Permalink 08:17:41 pm, by truewill Email , 49 words, 301 views   English (US)
Categories: Testing, Agile, C#

Good day

Today was a really productive day. I’ve been doing a lot of analysis lately and too little coding. Today I worked on a task that I thought would be (a) time-consuming and (b) difficult to test. It turned out to be neither.

Test-first development and LINQ made my day! :)

04/01/11

Permalink 06:56:47 pm, by truewill Email , 235 words, 187 views   English (US)
Categories: C#, .NET

.NET bug?

I reported a bug in the .NET 4 Framework (technically the BCL) today. I’m still not sure if it is a bug.

Here’s a LINQPad program that shows it:

void Main()
{
    string path =
      (new Uri(Assembly.GetExecutingAssembly().CodeBase))
      .AbsolutePath;

    // path = Path.GetFullPath(path);

    Console.WriteLine(path);

    Type t =
          Assembly.LoadFile(path)
            .GetTypes()
            .Where(x => x.Name == "IFoo")
            .First();

    Console.WriteLine(t);
    Console.WriteLine(typeof(IFoo));
    Console.WriteLine(t == typeof(IFoo));
}

public interface IFoo {}

This prints:

C:/TEMP/LINQPad/bbcyralv/query_mqjugi.dll
typeof (IFoo)
typeof (IFoo)
False

Note the forward-slashes in the path. When loading an assembly dynamically and reflecting over it, the types appear to be equal but actually aren’t.

Now uncomment the line path = Path.GetFullPath(path):

C:\TEMP\LINQPad\bbcyralv\query_dhjbqv.dll
typeof (IFoo)
typeof (IFoo)
True

This converts the path to use backslashes. (LINQPad generates a temporary assembly, which is why the DLL name changed.) Now the types are equal!

I was writing this screwy code to unit test code that was using reflection and dynamic assembly loading. That’s why it’s attempting to reload its own assembly.

In the end I changed the code to use Assembly.GetExecutingAssembly().Location, which worked with both ReSharper’s test runner (where the other code seemed to be picking up the “wrong” DLL) and TeamCity.

Update - April 11, 2011

Microsoft considers this By Design. Personally I’d like to see it either (a) throw or (b) work.

Development Central

Development Central is the blog of Bill Sorensen, a professional software developer. Much of this will relate to C#, .NET, and OOP in general.

Disclaimer
These postings are provided "AS IS" with no warranties and confer no rights.

Search

Categories

Linkblog

b2evolution

contributors

XML Feeds

What is RSS?

Who's Online?

  • Guest Users: 4

powered by b2evolution free blog software