Sunday, 15 April 2012

Unit testing ServiceStack REST webservices -


I tried to test some of the unit's attempts to test the rest of the service, but to no avail.

  1. The indexed post, by creating DirectServiceClient, but the "no method not implemented" exception has been raised.

  2. Turned the web service directly and called any () method, it worked, but it kills DB. I started the connection in the setup as below but how I duplicate the connection object and I do not know the in-memory client object is set.

    _dbConnection = New OrmLiteConnectionFactory (TestConfig.ConnectionString, SqlServerDialect.Provider). Open DB Connection ();

    Customer Service

      Public Class Customer Service: Service {Private Readonly IDbConnection _dbConnection; Public Customer Service (IDbConnection dbConnection) {_dbConnection = dbConnection; } Public Items Any (Customer Request Request) {If (request.id == Faucet) {New Logic Expression ("ID Required"); } Var customer = _dbConnection.QueryDapper & lt; Customers & gt; ("GetCustomer", new {@ id = request.id}). ToList (); Return customer; } [Route ("/ Customer / {ID}")] Public Category Customer: Request:   

    Request

      [Route ("/ Customer") ] IReturn & LT; Customers & gt; {Public string id {get; Set; }}    

    This is the only test that I can write for this method I am

    Ozzehko's answer will give the examination of code path that throws an exception. If you want to test the code path that runs before the exception, you will need to copy / stub the _dbConnection .QueryDapper call

    Expand in detail in Ozkazio's answer "Any_CalledWithAnId_ReturnsCustomer ()" ' By adding and all the 'celebrations' I have modified some of the code in the code provided by you. Instead of joking / changing IDbConnection for the new test, I have just created an InMemoryTestDatabase for testing. Don "t use it against its own database because it wipes out the 'Client' table for testing. [TestFixture] Public Class CustomerServiceTest { Public IDbConnectionFactoryInMemoryTestDatabase; [Set Up] Public Zero Setup (InMemoryTestDatabase = New OrmLiteConnectionFactory ("c: \\ testData.db", SqliteDialect.Provider);} [Test] Public Zero Any_CalledWithoutId_ExpectThrowArgumentException () {var db = MockRepository.GenerateStub & Lt; IDbConnection & gt; (); Var Requests = New Customer Service (); var Service = New Customer Service (DB); Responsibility Q & A; Logic Option & gt; ((= = Service) Any (Request));} [Test] Public Zero Any_CalledWithAnId_ReturnsCustomer () // Make your data request long ; (Var con = InMemoryTestDatabase.OpenDbConnection ()) {To make the customer.CreateTable & lt; Customer & gt; (true); // Force table and data to create the drop drop customer & lt; Customer & gt; (New Customer {FirstName = "Johnny", LastName = "Test"}); RequestId = con.GetLastInsertId ();} // Act var Rick Uest = new customer credits {id = (int) requestId}; Var service = new customer service (InMemoryTestDatabase.OpenDbConnection ()); Var result = (customer) service. Any (request); // Assign fears. Unsure (requestId, result.Id); Confirm. Erequal ("Johnny", results first name); }} [Route ("/ Customer / {ID}")) Public Category Customer Vacancy: iMartan & lt; Customers & gt; {Get public long id { Set; }} Public class customer {{AutoIncrement} public long id {get; Set; } Public string first name {receives; Set; } Public String LastName {get; Set; }} Public Sector Customer Service: ServiceStack.ServiceInterface.Service {Private Readonly IDbConnection _dbConnection; Public Customer Service (IDbConnection dbConnection) {_dbConnection = dbConnection; } Public object no (customer request) {if (request.id == null) {new argument expression ("id is required"); } Var customer = _dbConnection.QueryDapper & lt; Customers & gt; ("Choose ID from ID * @id", new {id = request.id}). ToList (); Return customer FirstOver Default (); }}

No comments:

Post a Comment