我正在对我的应用程序进行一些功能测试,我认为我已经非常接近了。我的问题是,当我运行第一个测试时,我得到了错误。
无法连接到远程服务器。
预期:好的
但是是:0
我可以确认,如果我在 Assert 上设置断点,然后尝试BaseUrl
在我的浏览器中点击,则找不到它。
这是我的测试。
[Test]
public void MyTestTest ()
{
var client = new RestClient( ServiceTestAppHostBase.BaseUrl );
// client.Authenticator = new HttpBasicAuthenticator( NUnitTestLoginName, NUnitTestLoginPassword );
var request = new RestRequest( "/users/", Method.GET );
request.RequestFormat = DataFormat.Json;
var response = client.Execute( request );
// do assertions on the response object now
Assert.That( response.StatusCode, Is.EqualTo( HttpStatusCode.OK ) );
}
AppServerTestSetup
看起来像这样
[SetUpFixture]
public class AppServerTestSetup
{
ServiceTestAppHostBase _appHost;
[SetUp]
public void SetUp()
{
_appHost = new ServiceTestAppHostBase();
_appHost.Init();
_appHost.Start(ServiceTestAppHostBase.BaseUrl);
}
[TearDown]
public void TearDown()
{
_appHost.Dispose();
}
}
ServiceTestAppHostBase
看起来像这样。
public class ServiceTestAppHostBase : AppHostHttpListenerBase
{
public const string BaseUrl = "http://localhost:8082/";
public ServiceTestAppHostBase () : base( "OurApp.AppServer", typeof( UserServiceInterface ).Assembly ) { }
public override void Configure ( Container container )
{
JsConfig.EmitCamelCaseNames = true;
SetConfig( new EndpointHostConfig
{
MapExceptionToStatusCode = {
{ typeof( NotFoundException ), 404 }, // Map exception to 404 not found http response.
{ typeof( SystemAccountChangeException ), 405 } // Map exception to 405 method not allowed.
}
} );
// Shared Container Registration
AppHostContainerRegistrations.Register( container );
// Setup the database
var migrationRunner = container.Resolve<IMigrationRunner>();
migrationRunner.CreateDatabase();
migrationRunner.RunAll();
}
}
注意:我也在AppHostContainerRegistrations
主应用程序中使用,它正在工作。我还验证了它正在测试设置中运行。
(AppHostContainerRegistrations
供参考)看起来像这样。
public class AppHostContainerRegistrations
{
public static void Register(Container container)
{
// IOC Registration
// Register base connection config
var dbConnection = ConfigurationManager.ConnectionStrings["databaseConnection"];
var databaseName = ConfigurationManager.AppSettings["databaseName"];
// Register Sqlserver DbProvider
container.Register<IDbConnectionProvider>( containr => new DbConnectionProvider( dbConnection.ConnectionString, dbConnection.ProviderName ) );
container.Register<IDbProvider>( containr => new DbProvider( containr.Resolve<IDbConnectionProvider>(), databaseName ) );
// Register repositories
container.RegisterAs<DatabaseVersionRepository, IDatabaseVersionRepository>();
container.RegisterAs<UserRepository, IUserRepository>();
container.RegisterAs<GroupRepository, IGroupRepository>();
container.RegisterAs<DeviceRepository, IDeviceRepository>();
container.RegisterAs<SecuritySettingsRepository, ISecuritySettingsRepository>();
// Register services
container.RegisterAs<UserService, IUserService>();
container.RegisterAs<GroupService, IGroupService>();
container.RegisterAs<SecuritySettingsService, ISecuritySettingsService>();
// Register everything else
container.RegisterAs<PasswordHasher, IPasswordHasher>();
container.RegisterAs<MigrationRunner, IMigrationRunner>();
container.Register( new UserModel { Id = new Guid( "6C83DDEC-5E58-4F28-BDE2-61EBF1B49691" ) } );
}
}
我们像这样进行 Db 设置的原因是因为我们在 App.Config 中有一个连接字符串和 db 名称,并且我们在部署期间依赖 Transforms 来设置数据库。
谁能帮我解决这个问题?