Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to unit test ServiceStack?
    primarykey
    data
    text
    <p>I love SS but I'm scratching my head trying to unit test my business layer. I'm new to unit testing andmocking and been reading up on NSubstitute as this looks like a fun mocking layer.</p> <p>I have my file structure roughly like this:</p> <pre><code>MainAppHostProject* | -AppStart -AppHost &lt;-- standard apphost DtoProject* | -HelloWorldDto &lt;-- simple POCO to ServiceLayerProject* | -HelloWorldService &lt;-- service interface that merely passes/sends Dtos to/from business layer BusinessLayerProject* | -HelloWorldManager &lt;-- 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 &lt;-- POCO of table structure </code></pre> <p>The Apphost points to the HelloWorldService assembly and registers the SQL Server database as standard.</p> <p>Everything actually works great and I have been able to build up the logic in a cleaner way. Unfortunately I wish to embark on unit testing BUT I dont know how to decouple the database. </p> <p>I tried to register a fake in memory database but then I think there's incompatibility issues with how I've used code to get identities etc in SQL Server vs SQLite ways.</p> <pre><code>// container.Register&lt;IDbConnectionFactory&gt;(c =&gt; new OrmLiteConnectionFactory(":memory:", false, SqliteOrmLiteDialectProvider.Instance)); // container.Register&lt;IDbConnectionFactory&gt;(c =&gt; new OrmLiteConnectionFactory(":memory:", false, SqlServerDialect.Provider)); </code></pre> <p>I just want to decouple and unit test. Any ideas please?</p> <p>***UPDATE</p> <pre><code>public class UnitTest1 { private Container container; [TestMethod] public void TestMethod1() { container = new Container(); // container.Register&lt;IDbConnectionFactory&gt;(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&lt;IDbConnectionFactory&gt;(c =&gt; new OrmLiteConnectionFactory(connectionString, SqlServerDialect.Provider)); // dependencies are injecting ok container.RegisterAutoWiredAs&lt;FeedbackRepo, IFeedbackRepo&gt;(); // service is autowiring --&gt; leading to good injections container.RegisterAutoWired&lt;FeedbackService&gt;(); var service = container.Resolve&lt;FeedbackService&gt;(); service.SetResolver(new BasicResolver(container)); // unit test is working well var request = new DTO.FeedbackDto { Message = "test" }; bool result = service.Post(request); } } </code></pre> <p>At the moment trying to get 'Db' to stop being null in my derived Service classes.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload