Today I had a weird problem at work. I’ve been working with a new team and they’ve been writing unit tests using FakeItEasy when they got a strange error message:
System.BadImageFormatException: … The signature is incorrect.
Now I’ve been using FakeItEasy for some time and I never once saw this strange behavior.
Googling for the problem only showed a bunch of solved issues but no solution to my specific problem.
Since I needed to solve this problem – I took a closer look at the code in question - the test code looked something like this:
[TestMethod]
public void Test()
{
    var fakeDependency = A.Fake<IMyDependency>();

    var classUnderTest = new MyClass();
    classUnderTest.Run(fakeDependency);

    A.CallTo(() => fakeDependency.SomeMethod()).MustHaveHappened();
}
The exception in question happened on the last line where we’ve used FakeItEasy to verify/assert that a method call occurred. Looking at the problematic interface didn’t help much:

public interface IMyDependency
{
    DataClass SomeMethod();
}
Until they tried changing SomeMethod’s return value to an object which caused the test to run properly.
This strange story made me think that perhaps the problem was the method’s return value (duh) and so I’ve asked in which assembly DataClass was declared. As it turned out it was in another assembly – not the one we were testing (the one with  MyClass inside).

image

And then we understood – since the test didn’t have a reference to AnotherAssembly.dll FakeItEasy had a little problem settting behavior on it or verifying method class on method that returns DataClass .

The fix was simple – add a reference to the assembly with DataClass.

BTW
If that machine had Resharper installed we would have found the problem quicker:

image

Sad but true, in any case once the reference thing was sorted out the test run smoothly.

3 Windows Phone 8.1 features I’m already using

No comments

Friday, April 18, 2014

Two days ago I couldn’t take it anymore…
There was a new and shiny (dev) release for my Windows Phone and I just had to install it. With trembling hands and a heavy heart I’ve installed Preview for Developers on my HTC 8x and dived right into the dreaded “download –> install –> reboot”  cycle which took forever (or 30 min – hard to tell). After the final 8.1 installation finished “importing” my stuff I’ve tried turning my phone on and was genuinely amazed that it worked without a hitch.
I very was impressed – here I am using a non-Nokia phone and installing a preview release and it just worked!
After less then a day as a WP8.1 user I discovered three new capabilities/apps that I always wanted and found their way to the new OS – thank you Microsoft for listening.
I could talk about Cortana – but there have been  numerous blog posts and articles about that specific feature and besides it’s not (yet?) available for all countries (including the one I’m  writing this post from).
And so without further ado – here are my top three new things WP8.1:

Podcastpodcastss

WP8 had “Podcasts” but it was lacking. It was available via the Store  whole experience felt half baked. Due to arcane (legal?) reasons if you happen to live outside of a few selected counties (US) you couldn’t subscribe to and of the freely available podcasts. I had to download the podcasts using ITunes (?!) and sync the downloaded files using the Windows Phone App for Desktop.
Those days are over – now I have a shiny new app where all of my podcast reside.wp_ss_20140417_0006 wp_ss_20140417_0005wp_ss_20140417_0007
I can even pin my favorites podcast to the start screen – although no notification has been observed.

Notifications center

Another high profile feature on the new OS is known as the notification center. This is where all of your phone notification would appear in a form which start to become de-facto standard in mobile operating systems. wp_ss_20140417_0002
But this is not what I like most about it – check out the top part of the notification center:
image
That’s right! no more searching the setting just to turn my Bluetooth on or connect to a new Wi-Fi network. Doing these common tasks has just become much simpler and to top it there’s even a link to the “All Settings” page.

Calendar

I always felt that my phone calendar left much to be desired. It was impossible to use – month view was too top level and impossible to read while finding a meeting in the daily view was a real pain. These two view are still there (I guess we need to leave something for the 8.2 release) but the weekly view become so much better:
wp_ss_20140417_0004
It’s easy to see my weekly and daily meetings and I like the fact that the bottom left part was left for a “high level” monthly view.
It’s seemly to see my schedule as well as next week’s plans and to top it all they even added a weather forecast for the day (also in daily view) image.

Conclusion

There are many features and new apps bundled in the 8.1 release but for me the features above are the “instant win” features – I’ve been using them immediately after the install finished (+ reboot) and I liked what I saw.
Although the WP8.1 is supposed to be released sometimes soon - I know I’ll be keeping my “Dev” version until it happens because I’m already hooked.

Faking a long running asynchronous call

No comments

Monday, April 07, 2014

A few days ago I needed to make sure that a specific method would only get called once no matter how many times it’s caller is invoked. The simplified code looked something like this:
public async Task CallLongOperation()
{
    var result = await _client.LongWebCall();

    if (result.Failed)
    {
        // log failure and exit
    }    
}
I needed to make sure that the client was invoked just once no matter how many times CallLongOperation gets called – and to make things interesting the client’s method is asynchronous. Usually when I want to fake a long running call I use WaitHandle but in this case I had something even better…

During previous work I’ve used TaskCompletionSource in order to enable async calls where needed and it seemed only logical that the same method would work for my unit tests:
[TestMethod]
public void CallLongOperation_DuringOperationCallAgain_CalledOnlyOnce()
{
    var fakeClient = A.Fake<IClient>();

    var completionSource = new TaskCompletionSource<Result>();
    A.CallTo(() => fakeClient.LongWebCall()).Returns(completionSource.Task);

    var cut = new ClassUnderTest(fakeClient);
    
    cut.CallLongOperation();
    cut.CallLongOperation();

    completionSource.SetResult(new Result());
    A.CallTo(() => fakeClient.LongWebCall()).MustHaveHappened(Repeated.Exactly.Once);
}
Using the TaskCompletionSource (line 6) help me simulate an async call that won’t return until I call SetResult on it (line 14) and since I do not muse await on the actual call I write the whole test without creating any additional threads – creating a simple and deterministic unit test.
It amazed me on how simple it become testing methods that uses async/await - just because the code uses asynchronous calls does not mean that its unit tests need to be complicated or unpredictable.

Happy coding...
Related Posts Plugin for WordPress, Blogger...