¿Cómo hacer la prueba unitaria de ServiceStack?
Me encantan las SS, pero me estoy rascando la cabeza al intentar realizar una prueba unitaria de mi capa de negocios. Soy nuevo en pruebas de unidad y burlas, y he estado leyendo sobre NSubstitute ya que parece una divertida capa burlona.
Tengo mi estructura de archivos más o menos así:
MainAppHostProject*
|
-AppStart
-AppHost <-- standard apphost
DtoProject*
|
-HelloWorldDto <-- simple POCO to
ServiceLayerProject*
|
-HelloWorldService <-- service interface that merely passes/sends Dtos to/from business layer
BusinessLayerProject*
|
-HelloWorldManager <-- logic to construct response and this class extends 'Service' (letting me access Db, session, etc)...sidenote: maybe i shouldve called this a HelloWorldRepository?
-CustomAuthProvider
-CustomUserSession
DaoProject*
|
-HelloWorldDao <-- POCO of table structure
Apphost apunta al ensamblado HelloWorldService y registra la base de datos de SQL Server como estándar.
Todo funciona realmente bien y he podido desarrollar la lógica de una manera más limpia. Desafortunadamente, quiero embarcarme en pruebas de unidad, PERO no sé cómo desacoplar la base de datos.
Intenté registrar una base de datos de memoria falsa, pero luego creo que hay problemas de incompatibilidad con la forma en que he usado el código para obtener identidades, etc. en las formas de SQL Server vs SQLite.
// container.Register<IDbConnectionFactory>(c => new OrmLiteConnectionFactory(":memory:", false, SqliteOrmLiteDialectProvider.Instance));
// container.Register<IDbConnectionFactory>(c => new OrmLiteConnectionFactory(":memory:", false, SqlServerDialect.Provider));
Solo quiero desacoplar y hacer la prueba unitaria. ¿Alguna idea por favor?
***ACTUALIZAR
public class UnitTest1
{
private Container container;
[TestMethod]
public void TestMethod1()
{
container = new Container();
// container.Register<IDbConnectionFactory>(new OrmLiteConnectionFactory(":memory:", false, SqliteDialect.Provider));
// sqlite didnt work so attempting with a real DB for now
var connectionString = @"Data Source=.\SQLEXPRESS;Initial Catalog=XXX;Integrated Security=True";
container.Register<IDbConnectionFactory>(c => new OrmLiteConnectionFactory(connectionString, SqlServerDialect.Provider));
// dependencies are injecting ok
container.RegisterAutoWiredAs<FeedbackRepo, IFeedbackRepo>();
// service is autowiring --> leading to good injections
container.RegisterAutoWired<FeedbackService>();
var service = container.Resolve<FeedbackService>();
service.SetResolver(new BasicResolver(container));
// unit test is working well
var request = new DTO.FeedbackDto { Message = "test" };
bool result = service.Post(request);
}
}
En este momento, trato de obtener 'Db' para dejar de ser nulo en mis clases de servicio derivadas.