One of the many great things about developing for the SalesLogix Windows client using .NET Extensions is that you are able to unit test your code. However, since .NET Extensions are passed references to the running instance of SalesLogix, it’s important to be able to mock those objects so you can test properly. Best of all, this also allows you to fully test and debug within Visual Studio without the need to have SalesLogix even running.

Background

I posted before about how to debug a .NET Extension by connecting to the running instance of SalesLogix and being able to set break points in your code. That’s great, but this also means you need to add your extension to SalesLogix, add code in SalesLogix to load the extension and so on. Being able to perform this same sort of testing inside of Visual Studio, taking SalesLogix out of the picture, let’s you focus completely on your .NET code and what the .NET Extension is actually doing.

Mocking the SlxApplication, BasicFunctions, and Other SalesLogix COM Objects

So what exactly are mock objects? There are plenty of resources online so I won’t get too deep into mocking in this post. In a nutshell, a mock object is a “fake” object meant to be used in place of some other object for testing. In the case of SalesLogix, your .NET Extension receives a COM reference to SlxApplication where it can get things like a connection string or use things in BasicFunctions, etc. What we are going to do is create a mock, or fake, SlxApplication object that provides the limited functionality we’ll be using in our .NET Extension. Then when we test we’ll use that mock object instead. We will give it to our .NET Extension rather than SalesLogix passing it into our extension. To accomplish this we’ll use the awesome Moq mocking library. Also, to point out, I will assume use of MSTest (even though I prefer XUnit).

First create a test project and add a test class. We will create our mock objects in the class initialize and then use them in separate test methods.

Now you have a mock SlxApplication object you can use and pass to your .NET Extensions. The way we set it up you’ll have the following available.

SlxApplication.ConnectionString

SlxApplication.BasicFunctions.CurrentUserID()

You could of course wire up more items if your extension used them.

Using the Mock Objects and Running Your .NET Extensions from Visual Studio

One of the best parts of setting up these mock objects (aside from the fact that you can create unit tests that won’t fail that reply on a running SalesLogix) is that you can now set up a test method to run your .NET Extension and use it from Visual Studio. What I like to do is create a test method that launches the extension and pass my mock objects to it.

If your .NET Extension is a form (meaning your form implements IRunnable), you can add the following test method.

Then, all you have to do is select that test method and click the “Run tests in current context” button in Visual Studio and your .NET Extension form will launch and it will have been passed the SlxApplication mock object and all will proceed as normal.

If your .NET Extension is a UserControl (meaning your UserControl implements IRunnable), you can add the UserControl to a test form (in the example below the form is called ExtensionTestForm) and then add the following test method.

Same as before, select the method and use the “Run tests in current context” and the test form will open with your UserControl on it and will appear as though it were embedded in SalesLogix.

Wrapping It All Up

When all is said and done, I love being able to unit test my applications, but I have to admit, my favorite part is being able to simply run my .NET Extensions from Visual Studio without the need to add them to the .NET Extension Manager, do a full refresh in SalesLogix, and get to the area my extension loads on a record I can test with. Doing things this way gives you complete control.

Ryan Farley is the Director of Development for Customer FX and creator of slxdeveloper.com. He's been blogging regularly about SalesLogix since 2001 and believes in sharing with the community. He loves C#, Javascript, Python, web development, open source, and Linux. He also loves his hobby as an amateur filmmaker.