crm单元测试使用

Action使用

使用paramBag传递入参,填写入参名,入参值,后使用 serviceProvider传入插件。

 

Assert.AreEqual(this.output["state"].ToString(), "success", "执行失败");

断言AreEqual("条件","结果","成功","失败");

/// <summary>
        /// 执行其他环境
        /// </summary>
        public PluginTest() : base(EnvironmentType.DEV) { }

        [TestMethod]
        [Description("模拟测试Action")]
        public void PluginTemplateTest1()
        {
            var paramBag = new Dictionary<string, object>();
            paramBag.Add("inputPara1", "1000001");
            paramBag.Add("inputPara2", "parameter");

            var serviceProvider = ConstructServiceProvider(paramBag);


            //引用插件DLL,执行单元测试
            //PluginTemplate test = new PluginTemplate("", "");
            //test.Execute(serviceProvider);

            Assert.AreEqual(this.output["state"].ToString(), "success", "执行失败");
        }

 

  使用单元测试需要在“C:\CRMConfig”目录下增加“Vyung.Xrm.config”文件并配置

 <crmConfig>
      <add key="CRMDEVConnectionString" value="Url=url.com; Domain=域名; Username=账户名; Password=密码; AuthType=连接方式" />    
  </crmConfig>

 

  key默认为“CRMConnectionString”,需要在增加DEV,

Plugin使用

对比Action,Plugin中增加了前后镜像的传递和操作的模拟,以及模拟当前操作人

镜像传递与入参相同

操作使用base执行插件中的操作,及镜像前后的参数

RegisterUserId填写操作人id模拟使用者

使用try catch捕获操作问题,执行判断

[TestMethod]
        [Description("模拟测试Plugin")]
        public void PluginTemplateTest2()
        {
            /* 当前模拟更新后操作,界面执行提交操作new_status改成30,
             * 插件已注册前后镜像  */
            var paramBag = new Dictionary<string, object>();
            var id = Guid.Parse("当前单据ID");

            //模拟前镜像 后镜像
            var preImage = serviceProxy.Retrieve("account", id, new ColumnSet(true));
            var postImage = serviceProxy.Retrieve("account", id, new ColumnSet(true));
            postImage["new_status"] = new OptionSetValue(30);

            //模拟Target操作
            var target = new Entity("account", id);
            target["new_status"] = new OptionSetValue(30);
            paramBag.Add("Target", target);

            //模拟插件操作
            base.RegisterSteps(MsgType.Update, 40);
            base.RegisterPreImage(preImage);
            base.RegisterPostImage(postImage);

            //模拟当前操作人context.userid.
            //不使用此方法默认当前代理用户权限.
            base.RegisterUserId("操作人Guid");

            var serviceProvider = ConstructServiceProvider(paramBag);

            //引用插件DLL,执行单元测试
            //PluginTemplate test = new PluginTemplate("", "");
            //test.Execute(serviceProvider);
        }

Plugin和Action在使用前,要使用用单元测试将预想场景模拟成功,捕获预想错误,才能注册

Console一样,执行成功后再部署

  

Console使用

与控制台中的Program类似,选择执行的程序后运行

添加执行日志,使用try catch捕获问题

[TestMethod]
        public void TaskTemplateTest()
        {
            //"TaskTemplate":任务名称, "para01":参数值,"para02":参数值
            string[] args = new string[] { "TaskTemplate", "para01", "para02" };
            TaskContext taskContext = new TaskContext();
            string taskCode = args[0];
            try
            {
                List<string> cmdarg = new List<string>();
                if (args.Length > 1)
                {
                    cmdarg.AddRange(args.Skip<string>(1));
                }
                ConfigFactory config = new ConfigFactory(ConfigurationManager.AppSettings["xrmConfig"].ToString());
                taskContext.Args = args;
                taskContext.CrmService = new CrmServiceClient(config.GetCRMConnectionString());
                taskContext.DbConnection = new SqlConnection(config.GetDBConnectionString());
                //taskContext.Logger = new FileLogger(config.GetNLogPath());//配置文件Log
                //执行任务
                //taskContext.Logger.ConsoleInfo($"{taskCode}程序启动时间:{DateTime.Now.ToString() }");


                //引用控制台DLL,执行单元测试
                //ITask taskName = TaskFactory.CreatTaks(taskCode);
                //taskName.Execute(taskContext);


                // taskContext.Logger.ConsoleInfo($"{taskCode}程序结束时间:{DateTime.Now.ToString() }");
            }
            catch (FaultException<OrganizationServiceFault> ex)
            {
                // taskContext.Logger.ConsoleError($"{taskCode}程序运行失败:Message:{ex.Message}", ex);
                throw;
            }
        }

  

 

posted @ 2022-01-19 16:33  整只龙虾  阅读(90)  评论(1编辑  收藏  举报