对Recorded Item动态调用OM Methods进行测试,界面如下:
输入Site的URL,File在SharePoint中的相对URL,并选择OM操作类型,点击Test进行测试。代码如下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using Microsoft.SharePoint; namespace RecordsOMTest { public partial class Form1 : Form { public Form1() { InitializeComponent(); OMMethods_comboBox.Items.Add("Rename"); OMMethods_comboBox.Items.Add("File item delete"); OMMethods_comboBox.Items.Add("File delete"); OMMethods_comboBox.Items.Add("File item recycle"); OMMethods_comboBox.Items.Add("File recycle"); OMMethods_comboBox.Items.Add("File undo check out"); OMMethods_comboBox.Items.Add("File check in"); OMMethods_comboBox.Items.Add("File check out"); } public delegate void CallOMMethod(SPFile file); private void test_button_Click(object sender, EventArgs e) { /* Block delete test. */ //Rename test. CallOMMethod Rename = file => { file.Item["Name"] = "kkk.txt"; file.Item.Update(); }; //Delete test 1. CallOMMethod delete1 = file => { file.Item.Delete(); }; //Delete test 2. CallOMMethod delete2 = file => { file.Delete(); }; //Delete test 3. CallOMMethod delete3 = file => { file.Item.Recycle(); }; //Delete test 4. CallOMMethod delete4 = file => { file.Recycle(); }; /* Block edit and delete test. */ //UndoCheckOut test. CallOMMethod UndoCKOut = file => { file.UndoCheckOut(); }; //CheckIn test. CallOMMethod CkIn = file => { file.CheckIn("Check in."); }; //CheckOut test. CallOMMethod CkOut = file => { file.CheckOut(); }; switch (OMMethods_comboBox.Text) { case "Rename": //Rename test. DoOMTest(Rename); break; case "File item delete": DoOMTest(delete1); break; case "File delete": DoOMTest(delete2); break; case "File item recycle": DoOMTest(delete3); break; case "File recycle": DoOMTest(delete4); break; case "File undo check out": DoOMTest(UndoCKOut); break; case "File check in": DoOMTest(CkIn); break; case "File check out": DoOMTest(CkOut); break; } } private void DoOMTest(CallOMMethod cm) { using (SPSite site = new SPSite(siteUrl_textBox.Text)) { SPWeb web = site.RootWeb; SPFile file = web.GetFile(filePath_textBox.Text); try { cm.Invoke(file); //Reach this step means test failed. result_richTextBox.Text = "Test failed.\r\n"; result_richTextBox.SelectionColor = Color.Red; } catch (Exception ex) { result_richTextBox.Text = "Test passed.\r\n"; result_richTextBox.Select(0, result_richTextBox.Text.Length); result_richTextBox.SelectionColor = Color.Green; result_richTextBox.Text += string.Concat(ex); } } } } }
提取每个OM Methods单元测试中的公共部分写成DoOMTest(CallOMMethod cm)方法,对于每个测试中不同的部分通过调用一个委托方法(橙色字体)来代替。然后我们对委托方法的类型进行声明(委托方法需要传入一个SPFile类型的参数):
public delegate void CallOMMethod(SPFile file);
再根据每个测试中的不同部分内容对委托进行实例化:
//Rename test. CallOMMethod Rename = file => { file.Item["Name"] = "kkk.txt"; file.Item.Update(); }; //Delete test 1. CallOMMethod delete1 = file => { file.Item.Delete(); }; //Delete test 2. CallOMMethod delete2 = file => { file.Delete(); }; //Delete test 3. CallOMMethod delete3 = file => { file.Item.Recycle(); }; //Delete test 4. CallOMMethod delete4 = file => { file.Recycle(); }; //UndoCheckOut test. CallOMMethod UndoCKOut = file => { file.UndoCheckOut(); }; //CheckIn test. CallOMMethod CkIn = file => { file.CheckIn("Check in."); }; //CheckOut test. CallOMMethod CkOut = file => { file.CheckOut(); };
这就是委托在测试中的妙用,极大的减少了重复代码量。
时间: 2024-10-21 17:38:36