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.


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.

[TestClass]
public class ExtensionTests
{
    ISlxApplication _slxApplication;

    [ClassInitialize]
    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.

[TestMethod]
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.

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

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
   Evaluating strings in C# Code snippets
When checking for two (or more) different values for a property in a code snippet, you can't simply l
Posted on Dec 13, 2013 by Jason Buss to Jason Buss' Blog
 
   Updates to the SalesLogix Mobile Developer Toolkit
I've released some updates to the SalesLogix Mobile Developer Toolkit. This new release adds some fix
Posted on Apr 10, 2013 by Ryan Farley to Ryan Farley's Blog
 
   Free Tool to Retrieve SalesLogix User Names and Passwords
As a business partner we work with customer SalesLogix databases often. Along with getting a database bac
Posted on Feb 25, 2013 by Ryan Farley to Ryan Farley's Blog
 
   Introducing the SalesLogix Mobile Developer Toolkit
I am very pleased to announce a new & free open source tool from Customer FX named the SalesLogix Mob
Posted on Nov 13, 2012 by Ryan Farley to Ryan Farley's Blog
 
   Exporting Table data via the SalesLogix web client
Recently, I had a request to create export functionality for a datagrid in the SalesLogix web client. I
Posted on Feb 22, 2012 by Jason Buss to Jason Buss' Blog
 
Comments

 

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

(required)  
(optional)
(required)  
Add
All contents Copyright © 2014 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