當我調試單元測試此代碼工作得很好:Design_Time_Address工作在調試器,但單位試運行期間不
[TestFixture]
public class Tests
{
private ServiceHost _host;
private Uri _baseAddress = new Uri(<code>"http://localhost:8733/Design_Time_Addresses/service"</code>);
private Inbound.Client.ServiceClient _client;
[SetUp]
public void Setup()
{
_host = new ServiceHost(typeof(MyService), _baseAddress);
var smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
_host.Description.Behaviors.Add(smb);
_host.Open();
_client = new Inbound.Client.ServiceClient();
}
[TearDown]
public void TearDown()
{
_client.Close();
_host.Close();
}
[Test]
public void Can_ping()
{
PingResponse response = _client.Ping(new PingRequest { Echo = "Hi" });
Assert.IsNotNull(response);
Assert.AreEqual("Hi", response.Echo);
}
}
然而,當我運行單元測試(右鍵單擊測試在VS測試資源管理器,運行選定的測試),我收到此錯誤信息:
"... System.ServiceModel.ServerTooBusyException :
The HTTP service located at http://localhost:8733/Design_Time_Addresses is unavailable. ...
----> System.Net.WebException : The remote server returned an error: (503) Server Unavailable."
看來,當NUnit的測試運行器運行測試,它無法啓動主機。我知道在VS安裝過程中,Design_Time_Address uri是在netsh中註冊的,我想知道是否應該爲NUnit測試運行器另外註冊。