Unit testing 如何模拟xml进行单元测试?
我需要对这个GetData方法进行单元测试Unit testing 如何模拟xml进行单元测试?,unit-testing,moq,mstest,moq-3,Unit Testing,Moq,Mstest,Moq 3,我需要对这个GetData方法进行单元测试 public MessageResponse GetData(XmlElement requestElement) { MessageResponse MsgResponse = new MessageResponse(); if (requestElement.Attributes["employeeNo"] == null){ MsgResponse.Messages
public MessageResponse GetData(XmlElement requestElement)
{
MessageResponse MsgResponse = new MessageResponse();
if (requestElement.Attributes["employeeNo"] == null){
MsgResponse.Messages = new List<string>();
MsgResponse.Messages.Add("Attribute employeeNo is missing");
MsgResponse.Error = true;
return MsgResponse;
}
if (requestElement.Attributes["xmlEmployeeName"] == null){
MsgResponse.Messages.Add("Attribute xmlEmployeeName is missing");
MsgResponse.Error = true;
return MsgResponse;
}
return MsgResponse;
}
为了测试它,首先我需要创建一个不带employeeNo的xml文件,创建另一个不带名称的文件,对于其他场景可能需要更多。看起来好像有很多工作要做。有没有更好的方法来测试它
我应该使用moq或其他测试框架来简化测试吗?您只需创建要测试的元素,而不必读取文件:
var doc = new XmlDocument();
doc.LoadXml("<MyTestElement/>");
var myTestElement = doc.DocumentElement;
myTestElement.Attributes["employeeNo"] = "fakeId";
var response = myTestResponder.GetData(myTestElement);
//assert whatever you need to
请查看一个很好的验证库
如果您采用上述方法,那么您的测试将更加简单。[TestMethod]
[TestMethod]
public void GetData_Returns_Correct_Message_When_EmployeeNo_Is_Null()
{
var inputWithoutEmployeeNo = GetElement(@"<input></input>");
var actual = GetData(inputWithoutEmployeeNo);
Assert.IsTrue(actual.Error, "Error should be true when employee no. is missing");
Assert.IsNotNull(actual.Messages);
Assert.AreEqual(1, actual.Messages.Count);
Assert.AreEqual("Attribute employeeNo is missing", actual.Messages[0]);
}
private XmlElement GetElement(string xml)
{
var doc = new XmlDocument();
doc.LoadXml(xml);
return doc.DocumentElement;
}
public void GetData_在_EmployeeNo_为_Null()时返回_Correct_Message_
{
var inputWithoutEmployeeNo=GetElement(@“”);
var actual=GetData(inputWithoutEmployeeNo);
Assert.IsTrue(actual.Error,“缺少员工编号时错误应为true”);
Assert.IsNotNull(实际的.Messages);
aresequal(1,实际的.Messages.Count);
Assert.AreEqual(“缺少属性employeeNo”,实际的.Messages[0]);
}
私有XmlElement GetElement(字符串xml)
{
var doc=新的XmlDocument();
doc.LoadXml(xml);
返回doc.document元素;
}
在进行单元测试时,我发现代码抛出了一个NullReferenceException。
以下单元测试演示了该问题:
[TestMethod]
[ExpectedException(typeof(NullReferenceException))]
public void GetData_Throws_NullReferenceException_When_EmployeeNo_Is_Not_Null_And_XmlEmployeeName_Is_Null()
{
var inputWithoutEmployeeNo = GetElement(@"<input employeeNo='123'></input>");
GetData(inputWithoutEmployeeNo);
}
[TestMethod]
[ExpectedException(typeof(NullReferenceException))]
public void GetData在\u EmployeeNo\u不是\u Null\u且\u XmlEmployeeName\u为\u Null()时抛出\u NullReferenceException\u
{
var inputWithoutEmployeeNo=GetElement(@“”);
GetData(输入时不带员工编号);
}
使用最小起订量
使用系统;
使用System.Xml;
使用最小起订量;
使用NUnit.Framework;
名称空间MockXmlTest
{
[测试夹具]
公共类MyServiceTests
{
私有MockSetup\u MockSetup;
[设置]
公共void Init()
{
_mockSetup=mockSetup.HappySetup();
}
[测试]
public void MyService\u应该\u返回\u Guid()
{
//安排
var myService=\u mockSetup.myService.Object;
var-id=42;
应为var=Guid.Empty.ToString();
//表演
var-actual=myService.GetXml(id);
//断言
aresequal(预期、实际、FirstChild.InnerText);
}
}
公共类MyService:IMyService
{
公共XmlDocument GetXml(int-id)
{
var doc=新的XmlDocument();
//做真正的事情
退货单;
}
}
公共接口IMyService
{
XmlDocument GetXml(int-id);
}
公共类模拟设置
{
公共模拟MyService{get;set;}
公共模拟设置()
{
MyService=newmock();
}
公共静态MockSetup HappySetup()
{
var mockSetup=新建mockSetup();
var mockDoc=CreateMockDoc();
//匹配整数的任何id,返回XmlDocument模拟
mockSetup.MyService.Setup(m=>m.GetXml(It.IsAny()).Returns(mockDoc);
返回模拟设置;
}
私有静态XmlDocument CreateMockDoc()
{
//00000000-0000-0000-0000-000000000000
XmlDocument mockDoc=新的XmlDocument();
xmlement el=(xmlement)mockDoc.AppendChild(mockDoc.CreateElement(“Main”));
el.AppendChild(mockDoc.CreateElement(“MyGuid”)).InnerText=It.IsAny().ToString();
返回文件;
}
}
}
就像您的想法一样,唯一的问题是实际的xml文件非常大。还有很多字段需要验证。它将是一个长xml字符串。
[TestMethod]
public void GetData_Returns_Correct_Message_When_EmployeeNo_Is_Null()
{
var inputWithoutEmployeeNo = GetElement(@"<input></input>");
var actual = GetData(inputWithoutEmployeeNo);
Assert.IsTrue(actual.Error, "Error should be true when employee no. is missing");
Assert.IsNotNull(actual.Messages);
Assert.AreEqual(1, actual.Messages.Count);
Assert.AreEqual("Attribute employeeNo is missing", actual.Messages[0]);
}
private XmlElement GetElement(string xml)
{
var doc = new XmlDocument();
doc.LoadXml(xml);
return doc.DocumentElement;
}
[TestMethod]
[ExpectedException(typeof(NullReferenceException))]
public void GetData_Throws_NullReferenceException_When_EmployeeNo_Is_Not_Null_And_XmlEmployeeName_Is_Null()
{
var inputWithoutEmployeeNo = GetElement(@"<input employeeNo='123'></input>");
GetData(inputWithoutEmployeeNo);
}
using System;
using System.Xml;
using Moq;
using NUnit.Framework;
namespace MockXmlTest
{
[TestFixture]
public class MyServiceTests
{
private MockSetup _mockSetup;
[SetUp]
public void Init()
{
_mockSetup = MockSetup.HappySetup();
}
[Test]
public void MyService_Should_Return_Guid()
{
//Arrange
var myService = _mockSetup.MyService.Object;
var id = 42;
var expected = Guid.Empty.ToString();
//Act
var actual = myService.GetXml(id);
//Assert
Assert.AreEqual(expected, actual.FirstChild.InnerText);
}
}
public class MyService : IMyService
{
public XmlDocument GetXml(int id)
{
var doc = new XmlDocument();
//Do real stuff
return doc;
}
}
public interface IMyService
{
XmlDocument GetXml(int id);
}
public class MockSetup
{
public Mock<IMyService> MyService { get; set; }
public MockSetup()
{
MyService = new Mock<IMyService>();
}
public static MockSetup HappySetup()
{
var mockSetup = new MockSetup();
var mockDoc = CreateMockDoc();
//Matches any id of an integer, returns a XmlDocument mock
mockSetup.MyService.Setup(m => m.GetXml(It.IsAny<int>())).Returns(mockDoc);
return mockSetup;
}
private static XmlDocument CreateMockDoc()
{
//<Main><MyGuid>00000000-0000-0000-0000-000000000000</MyGuid></Main>
XmlDocument mockDoc = new XmlDocument();
XmlElement el = (XmlElement)mockDoc.AppendChild(mockDoc.CreateElement("Main"));
el.AppendChild(mockDoc.CreateElement("MyGuid")).InnerText = It.IsAny<Guid>().ToString();
return mockDoc;
}
}
}