【发布时间】:2011-09-27 20:36:42
【问题描述】:
我正在尝试掌握起订量并使用一个简单的示例来解决这个问题。我正在使用 Google 对地址进行地理编码。我已经包装了 WebClient,所以它可以被嘲笑。代码如下:
public class Position
{
public Position(double latitude, double longitude)
{
Latitude = latitude;
Longitude = longitude;
}
public virtual double Latitude { get; private set; }
public virtual double Longitude { get; private set; }
}
public interface IWebDownloader
{
string Download(string address);
}
public class WebDownloader : IWebDownloader
{
public WebDownloader()
{
WebProxy wp = new WebProxy("proxy", 8080);
wp.Credentials = new NetworkCredential("user", "password", "domain");
_webClient = new WebClient();
_webClient.Proxy = wp;
}
private WebClient _webClient = null;
#region IWebDownloader Members
public string Download(string address)
{
return Encoding.ASCII.GetString(_webClient.DownloadData(address));
}
#endregion
}
public class Geocoder
{
public Position GetPosition(string address, IWebDownloader downloader)
{
string url = string.Format("http://maps.googleapis.com/maps/api/geocode/xml?address={0}&sensor=false",
address);
string xml = downloader.Download(url);
XDocument doc = XDocument.Parse(xml);
var position = from p in doc.Descendants("location")
select new Position(
double.Parse(p.Element("lat").Value),
double.Parse(p.Element("lng").Value)
);
return position.First();
}
}
到目前为止一切顺利。现在这里是 Moq 的单元测试:
[TestMethod()]
public void GetPositionTest()
{
Mock<IWebDownloader> mockDownloader = new Mock<IWebDownloader>(MockBehavior.Strict);
const string address = "Brisbane, Australia";
mockDownloader.Setup(w => w.Download(address)).Returns(Resource1.addressXml);
IWebDownloader mockObject = mockDownloader.Object;
Geocoder geocoder = new Geocoder();
Position position = geocoder.GetPosition(address, mockObject);
Assert.AreEqual(position.Latitude , -27.3611890);
Assert.AreEqual(position.Longitude, 152.9831570);
}
返回值位于资源文件中,是 Google 的 XML 输出。现在,当我运行单元测试时,我得到了异常:
mock 上的所有调用都必须有相应的设置..
如果我关闭严格模式,则模拟对象返回 null。如果我将设置更改为:
mockDownloader.Setup(w => w.Download(It.IsAny<string>())).Returns(Resource1.addressXml);
然后测试运行良好。但是我不想测试任何字符串,我想测试这个特定的地址。
请让我摆脱痛苦,告诉我哪里出错了。
【问题讨论】:
标签: unit-testing moq