Login / Register  search  syndication  about

          Ryan Farley's Blog

Ryan Farley on .NET Development with a focus on CRM Development for SalesLogix

Unit Testing SalesLogix .NET Extensions and Mocking the SlxApplication Object

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.


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.

public class ExtensionTests
    ISlxApplication _slxApplication;

    public static void ClassInitialize(TestContext testContext)
        // Create the mock SlxApplication object
        var slxApplication = new Mock<ISlxApplication>();
        // Set the connection string you want to use for your testing
        slxApplication.Setup(x => x.ConnectionString).Returns(TestConstants.SlxDbConnectionString);

        // Create the BasicFunctions mock object
        var basicFunctions = new Mock<IBasicFunctions>();
        // Set the userid you want to use as current user for your testing
        basicFunctions.Setup(x => x.CurrentUserID()).Returns(TestConstants.UserId);
        slxApplication.Setup(x => x.BasicFunctions).Returns(basicFunctions.Object);

        _slxApplication = slxApplication.Object;

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.

public void RunExtensionForm()
    var extensionForm = new ExtensionForm();
    extensionForm.Initialize(_slxApplication, null);
    extensionForm.Run(new[] { TestConstants.AccountId });

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.

public void RunExtensionUserControl()
    var extension = new ExtensionTestForm();
    extension.Initialize(_slxApplication, null);
    extension.Run(new[] { TestConstants.AccountId });

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.

And yes, all of this works with the NetExtensionsHelper library I posted about earlier without changes :-)

What's This?
Bookmark and Share

About Ryan Farley

   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.

View the SalesLogix Mobile Seveloper Series
View the SalesLogix SData Developer Series
View the Git for the SalesLogix Developer series

Related Content
   Creating a Searchable & Filterable ComboBox in Infor CRM (Saleslogix) Web Client
I've had a few posts lately covering the ComboBox control in the Infor CRM (Saleslogix) Web Client. T
Posted on Feb 05, 2015 by Ryan Farley to Ryan Farley's Blog
   How to Disable the ComboBox Control in the Infor CRM (Saleslogix) Web Client
There's a problem with the ComboBox control in the Infor CRM (Saleslogix) Web Client. It won't di
Posted on Jan 22, 2015 by Ryan Farley to Ryan Farley's Blog
   Easily Showing and Hiding Tabs at Runtime in Infor CRM (Saleslogix)
Showing and hiding tabs in the Infor CRM (Saleslogix) client should be an easy thing. It should be someth
Posted on Dec 16, 2014 by Ryan Farley to Ryan Farley's Blog
   Avoiding the Dirty Data Message When Programatically Redirecting to a Record in Infor CRM (Saleslogix)
In my last post I wrote about using the OnClientClick property of a control (or button) to run JavaScript
Posted on Dec 02, 2014 by Ryan Farley to Ryan Farley's Blog
   Confirming Actions With an "Are you sure?" Prompt in Infor CRM (Saleslogix)
I'm not a huge fan of bugging end users. I figure, if an end user clicked something, he or she meant
Posted on Nov 18, 2014 by Ryan Farley to Ryan Farley's Blog


Unit Testing of .Net Extension in SalesLogix LAN « SRNR Blog said:

Pingback from  Unit Testing of .Net Extension in SalesLogix LAN &laquo;  SRNR Blog

February 1, 2012 5:19 PM

Leave a Comment

All contents Copyright © 2015 Customer FX Corporation
Customer FX Corporation
2324 University Avenue West, Suite 115
Saint Paul, Minnesota 55114
Tel: 800.728.5783

  Follow @CustomerFX on twitter
Follow the best news, tips, and articles
  Subscribe to Customer FX on youtube
Watch SalesLogix tutorial videos from Customer FX
Login / Register

Upcoming Workshop Dates

No scheduled workshops