使用.Net WebRequest Factory
正如Richard Willis在http://blog.salamandersoft.co.uk/index.php/2009/10/how-to-mock-httpwebrequest-when-unit-testing/中建议的那样,我试图调用一个web请求行为。
为此(我问我是否在这里搞点什么)我实现了IWebRequestCreate并扩展了WebRequest和WebResponse。 (链接代码中有更多详细信息)
但现在在我的代码中,我有一个测试注册( WebRequest.RegisterPrefix
)前缀:
[Test]
public void Test() {
var some = File.ReadAllBytes(@"TestDataWebServiceadmrond_13jan2011_14jan2011.xml");
WebRequest.RegisterPrefix("mockPrefix", new WebRequestCreateMock());
WebRequestFake request = WebRequestCreateMock.CreateRequestFake(some);
_remoteRepository.PopulateWithMeterData(_meter);
... (error in line before)
然后,我得到这个错误: Invalid URI: The hostname could not be parsed.
但为什么? 在我的PopulateWithMeterData(Meter meter)
我有这个电话:
WebRequest request = WebRequest.Create(urlListMeteringData);
WebResponse ws = request.GetResponse();
一些建议? 在我的课程实施过程中有趣吗?
编辑:作为@Matthew问:
public class WebRequestCreateMock : IWebRequestCreate {
static WebRequest _nextRequest;
static readonly object LockObject = new object();
static public WebRequest NextRequest {
get { return _nextRequest; }
set {
lock (LockObject) {
_nextRequest = value;
}
}
}
public WebRequest Create(Uri uri) {
return _nextRequest;
}
public static WebRequestFake CreateRequestFake(byte[] xmlStream) {
WebRequestFake webRequestFake = new WebRequestFake(xmlStream);
NextRequest = webRequestFake;
return webRequestFake;
}
}
public class WebRequestFake : WebRequest {
MemoryStream requestStream = new MemoryStream();
MemoryStream responseStream;
public override string Method { get; set; }
public override string ContentType { get; set; }
public override long ContentLength { get; set; }
public WebRequestFake(byte[] response) {
responseStream = new MemoryStream(response);
}
public override Stream GetRequestStream() {
return requestStream;
}
public override WebResponse GetResponse() {
return new WebReponseFake(responseStream);
}
}
public class WebReponseFake : WebResponse {
private readonly Stream _responseStream;
public WebReponseFake(Stream responseStream) {
_responseStream = responseStream;
}
public override Stream GetResponseStream() {
return _responseStream;
}
}
Url类似于: mockPrefix://NoMatterUrl
由于错误是“无效的URI:无法解析主机名。” 你可能搞砸了你的Uri“mockPrefix:// NoMatterUrl”
我曾经遇到过这个问题,因为我忘记了在域uri和请求参数之间添加“/”。
你能准确地发布你的“NoMatterUri”吗?
你需要用冒号(':')注册你的前缀; 如下所示:
WebRequest.RegisterPrefix("mockPrefix:", new WebRequestCreateMock());
我发现有必要在前缀中包含尾随的“/”。 例如,“ test://localhost/
”:
[TestClass]
public class WebRequestTests
{
[TestMethod]
public void TestWebRequestCreate()
{
const string uriString = "test://localhost/foo/bar.baz?a=b&c=d";
var webRequest = new MockWebRequestCreateAssertUrl(uriString);
Assert.IsTrue(WebRequest.RegisterPrefix("test://localhost/", webRequest),
"Failed to register prefix");
Assert.IsNotNull(WebRequest.Create(uriString));
}
public class MockWebRequestCreateAssertUrl : IWebRequestCreate
{
private readonly Uri _expectedUri;
public MockWebRequestCreateAssertUrl(string uriString)
{
_expectedUri = new Uri(uriString);
}
public WebRequest Create(Uri uri)
{
Assert.AreEqual(_expectedUri, uri, "uri parameter is wrong");
return new MockWebRequestAssertUrl();
}
}
public class MockWebRequestAssertUrl : WebRequest {}
}
链接地址: http://www.djcxy.com/p/50833.html