Random Musings

May 30, 2009

TDD on Dynamics CRM Workflow

Filed under: Dynamics CRM — haditeo @ 8:23 pm
Tags: , ,

In the Dynamics CRM 4 custom workflow activity, we can create an instance of CRMService by instantiating a IWorkflowContext object

IWorkflowContext context = ((IContextService)executionContext.GetService(typeof(IContextService))).Context;
ICrmService crmService = context.CreateCrmService();

How to unit test a Dynamics CRM 4 workflow ? since the CrmService object needs to be created with an interface type of ICrmService

Notice here carefully that we need to implement several methods, but in my scenario, i need to use the execute method.

    class MyCrmService :ICrmService
    {

        private CrmService crmService;

        public MyCrmService()
        {
            NetworkCredential nc = new NetworkCredential();
            nc.Domain = "Domain";
            nc.UserName = "administrator";
            nc.Password = "pwd";

            // Set up the CRM Service.
            CrmAuthenticationToken token = new CrmAuthenticationToken();
            token.AuthenticationType = 0;
            token.OrganizationName = "xyz";

            CrmService service = new CrmService();
            service.Url = "http://localhost/mscrmservices/2007/crmservice.asmx";
            service.CrmAuthenticationTokenValue = token;
            service.Credentials = nc;
            this.crmService = service;        
        }


        #region ICrmService Members

        public Guid Create(BusinessEntity entity)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public void Delete(string entityName, Guid id)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public object Execute(object request)
        {
            return crmService.Execute((Request)request);
        }

        public string Fetch(string fetchXml)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public BusinessEntity Retrieve(string entityName, Guid id, ColumnSetBase columnSet)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public BusinessEntityCollection RetrieveMultiple(QueryBase query)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public void Update(BusinessEntity entity)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            throw new Exception("The method or operation is not implemented.");
        }

        #endregion
    [TestFixture]
    public class Class1
    {
        private MyCrmService crmSvc;
        private MyMetadataService metadataSvc;
        private SendEmailActivity activity;

        [SetUp]
        public void Init()
        {
            this.crmSvc = new MyCrmService();

            this.metadataSvc = new MyMetadataService();

            this.activity = new SendEmailActivity();

        }
     }

The advantages of using the Interface, during the runtime, we can switch a method which requires ICrmService with the actual CrmService itself

Advertisements

1 Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: