Upcoming speaking engagements: Vancouver here I come!

No comments

Monday, November 14, 2016

At the beginning of December I’m going to speak at ConFoo Vancouver.
Vancouver 2016 | December 5-7, 2016
I spoke at ConFoo Montreal in the past and I look forward to speaking at the first ever ConFoo Vancouver.
I’ll be speaking on two of my favorite topics – unit testing and electronics (not at the same session).
5 Unit Testing Facts I Wish I’d Known 7 Years Ago – on Tuesday (6th) . A session I’ve created as a kind of retrospective of what worked, and especially what didn’t work when I attempted to start unit test my code. I wanted to provide a few tips for software developer starting down the unit testing path so that they won’t fail just like I did oh so many years ago.
Electronics 101 for Software Developers – on Wednesday (7th). A fun session for all developers who want to start building cool IoT/electronics projects. I plan to start from the basics of electricity and explain how to use sensors and read circuit diagrams – it would be fun, informative and useful for anyone who ever wanted to build his own robot army:
WP_20161018_12_45_05_Pro
Before all that on Monday night (5/12) I’m going to speak at the Windows Platform Developer Group with Ariel Ben Horesh.
We’re going to do a two part meetup – I’ll talk about Visual Studio debugging tricks every .NET developer should know and Ariel is going to discuss how to build your company’s mobile development strategy in “Calling the Shots: Should I go Native or Cross Platform?
If you’re interested in learning how to quickly and effectively debug your code you should come – especially since you might receive an OzCode license.

So if you’re living in Vancouver or you’re just in town for ConFoo – come by and say hello.

With every new version the C# language has grown and improved. The last version so a.k.a C# 6 has brought some of my favorite features. And with C# 7 just around the corner I know there's more to come.

One of the new useful features added to C# is the ability to filter exceptions. Consider the following - completely (and poorly) invented class:

public class MyClass 
public class MyClass
{
    public void MyMethod(int count)
    {
        throw new MyException(1000 + count);
    }
}

If I need to exit only when an exception with a specific error code is thrown I can write the following code:

class Program
{
    static void Main(string[] args)
    {
        var myClass = new MyClass();

        for (int i = 0; i < 10; i++)
        {

            try
            {
                myClass.MyMethod(i);
            }
            catch (MyException ex) when (ex.ErrorCode == 1003)
            {
                Console.WriteLine($"Got the right exception in iteration: {i}");
                break;
            }
            catch (MyException ex)
            {
                Console.WriteLine($"Other exception: {ex.ErrorCode}");
            }
        }
    }
}

And each exception with value which is not 1003 will be swallowed. There are limitless scenarios in which this feature can be useful - for example have a look at Jimmy Bogard's post on how to avoid exceptions on the weekends.

But what happens when I need the same functionality but just for a specific debug session or if I'm working on a project which does not use C# 6 (yet?). For those cases I can use the magic of conditional breakpoints.

Filtering with conditional breakpoints

There are two ways to set a conditional breakpoint - the old way and the OzCode way.

If you never used them - you should; it's a time saving tool which will save you valuable debug time.

In order to use Conditional breakpoints just create a regular breakpoint at the exception's constructor and then add condition to that breakpoint - simple as that. With OzCode it's even easier:

  1. Create a "regular" breakpoint at the exception's constructor
  2. Run until breakpoint
  3. Open the quick watch window
  4. Choose the property you care about
  5. Update the condition
  6. Press "ok" and run

 

Conditional_breakpoints

Once you fix the bug you can delete the conditional breakpoint and continue working – or find and fix another bug.

Conclusion

Conditional breakpoints have been part of your favorite IDE of choice for ever and yet not all developers use them as often as they should. Catching just the right exception is one scenario where they help find and fix bugs and save us time in which we can develop new features for our customers or just grab another cup of coffee.

Faking DateTime using Microsoft Fakes seems to be broken

No comments

Tuesday, November 08, 2016

It seems as if causing DateTime.Now to return another value has become the demo to show when demoing an unconstrained Mocking framework. It’s easy to show and needed in many unit tests – unless you want your tests to be affected by time – trust me and you don’t.
That’s why I was amazed to find out that the demo from MSDN which showed how to use Shims has somehow stopped working…

What I initially did

In order to start using Microsoft Fakes you need to use Visual Studio Enterprise edition – I’ve used VS2015 update 5 in this case.
The next step is to right click on the assembly you need to fake and choose Add Fake Assembly
image
this will generate the same assembly followed .fakes so if you’re faking MyAssembly a new MyAssembly.Fakes will be created this is not the case with System as I was quickly reminded.
Now according to my knowledge and the demos at hand all I needed to do now is find ShimDateTime and use some magic to change how it behaved – but it was no where to be found – in fact I could not see anything to do with DateTime which I know for certain existed inside System.
It took me a while to get it but looking at the project’s references showed the problem:
image
You see – usually when faking System two fake assemblies are created – one for System and one for mscorlib where the good stuff is.
You can even see that under Fakes folder there are two configuration files one for each – but no mscorlib to be found.

The Solution?

I did what any developer would have done: I’ve decided to repeat the same process again expecting different results. Actually first I Googled a bit and found very little information.
And so I need to delete three files – the fakes assembly and two configuration files (fake configuration?) and try again – and I got the exact same mess.
After trying two more time – just to make sure I’ve decided to check the output window and saw the following:
image
Armed with this new knowledge I’ve decided to dig into some documentation and found that some new breaking changes caused issues when creating fakes and quick workaround would be to remove a few classes from the To fake list” and so I’ve opened mscorlib.fakes configuration file and written (read: pasted) the following code:
<Fakes xmlns="http://schemas.microsoft.com/fakes/2011/">
  <Assembly Name="mscorlib" Version="4.0.0.0"/>
  <StubGeneration>
    <Remove FullName="System.Diagnostics.Tracing"/>
    <Remove FullName="System.Text.Encoding"/>
    <Remove FullName="System.Security.Cryptography" />
  </StubGeneration>
</Fakes>
Next I’ve deleted System.4.0.0.0.Fakes and system.fakes and right-clicked System->Add Fakes Assembly and got the following:
image
And now I was able to write my test using my DateTime of choosing.

Frustrating? you bet! but at least now I know a quick workaround and able to test time based code – if all I have is Microsoft Fakes.
Last week my very first Pluralsight course went live!
image
My course on unit testing in C++ using CATCH is ready for your viewing pleasure. It’s about a topic I’m passionate about - how to write good unit tests, I’ve used Catch because I found that it helps writing good tests while avoiding many of the pitfalls that unit test practitioners might stumble on.
It was cool seeing the result of several months (more if you count the audition) of work.
Writing unit tests is a big part of being a professional software developer. This course would teach you how to write readable and robust unit tests using Catch, a simple, yet powerful unit testing framework.


I like to thank the good people of Pluralsight for helping me create this course and for the excellent feedback and all their hard work.
If you have any feedback or suggestion please let me know.
Related Posts Plugin for WordPress, Blogger...