Skip to content

Tutorial 2

PhuocLe edited this page Mar 7, 2018 · 13 revisions

Task

Create Unit Test project for plugin: Wooow.Kool.Plugin.Account

Coding

  1. Add PL.DynamicsCrm.DevKit > 26. C# ProxyTypes Project to your solution.
    • This project use by FakeXrmEasy
    • A popup form opened.
    • Select saved connection or create new connection.
    • Click OK
    • PL.DynamicsCrm.DevKit created project name: Wooow.Kool.ProxyTypes
  2. Rebuild solution to restore NuGet packages and check solution rebuild all succeeded.
  3. Open command-line, go to Wooow.Kool.ProxyTypes folder, then run run.bat and waiting CrmSvcUtil generate code to GeneratedCode.cs file.
  4. Rebuild solution without errors.
  5. Add PL.DynamicsCrm.DevKit > 16. C# Test Project to your solution.
    • A popup form opened.
    • Dropdown list all available projects test.
    • Select Wooow.Kool.Plugin.Account
    • Click OK
    • PL.DynamicsCrm.DevKit created test project name: Wooow.Kool.Plugin.Account.Test
  6. Add PL.DynamicsCrm.DevKit > 11. C# Test Class to Wooow.Kool.Plugin.Account.Test project.
    • A popup form opened.
    • Dropdown list all available test class
    • Select PreAccountCreateSynchronous
    • Click OK
    • PL.DynamicsCrm.DevKit created test class: PreAccountCreateSynchronousTest
  7. Rebuild solution without errors.
  8. Open Test Explorer window and Run All tests.
    • 4 passed tests.
    • 1 failed test.
  9. Edit failed test
[TestMethod]
public void PreAccountCreate_Test_AccountName_Is_Null()
{
    //setup
    Context.ProxyTypesAssembly = Assembly.GetAssembly(typeof(ProxyTypesAssembly));
    Context.Data.Clear();
    var target = new Shared.Entities.Account
    {
        AccountId = Guid.NewGuid()
    };
    PluginContext.InputParameters["Target"] = target.GetCreateEntity();

    //run
    var plugin = Context.ExecutePluginWith<PreAccountCreateSynchronous>(PluginContext);

    //result
    var resultTarget = (Entity)PluginContext.InputParameters["Target"];
    var account = new Shared.Entities.Account(resultTarget);
    Assert.IsNull(account.Name);
}

[TestMethod]
public void PreAccountCreate_Test_AccountName_UpperCase()
{
    //setup
    Context.ProxyTypesAssembly = Assembly.GetAssembly(typeof(ProxyTypesAssembly));
    Context.Data.Clear();
    var target = new Shared.Entities.Account
    {
        AccountId = Guid.NewGuid(),
        Name = "Hello World"
    };
    PluginContext.InputParameters["Target"] = target.GetCreateEntity();

    //run
    var plugin = Context.ExecutePluginWith<PreAccountCreateSynchronous>(PluginContext);

    //result
    var resultTarget = (Entity)PluginContext.InputParameters["Target"];
    var account = new Shared.Entities.Account(resultTarget);
    Assert.AreEqual(account.Name, "HELLO WORLD");
}
  1. Run All tests again.
    • 6 passed tests.
  2. Check-in all files to your source control.
  3. You finished this tutorial.

Your Solution Explorer after you finished this tutorial

Finished Tutorial 1

Clone this wiki locally