-
Notifications
You must be signed in to change notification settings - Fork 1.6k
HowTo write unit integration tests
Demis Bellot edited this page Oct 25, 2016
·
6 revisions
This page has moved to docs.servicestack.net
The CustomerRestExample.cs shows an example of a stand-alone integration test:
//Create your ServiceStack AppHost with only the dependencies your tests need
public class AppHost : AppSelfHostBase
{
public AppHost() : base("Customer REST Example", typeof(CustomerService).Assembly) {}
public override void Configure(Container container)
{
container.Register<IDbConnectionFactory>(c =>
new OrmLiteConnectionFactory(":memory:", SqliteDialect.Provider));
using (var db = container.Resolve<IDbConnectionFactory>().Open())
{
db.CreateTableIfNotExists<Customer>();
}
}
}
//Add Service Contract DTO's and Data Models
[Route("/customers", "GET")]
public class GetCustomers : IReturn<GetCustomersResponse> {}
public class GetCustomersResponse
{
public List<Customer> Results { get; set; }
}
[Route("/customers/{Id}", "GET")]
public class GetCustomer : IReturn<Customer>
{
public int Id { get; set; }
}
[Route("/customers", "POST")]
public class CreateCustomer : IReturn<Customer>
{
public string Name { get; set; }
}
[Route("/customers/{Id}", "PUT")]
public class UpdateCustomer : IReturn<Customer>
{
public int Id { get; set; }
public string Name { get; set; }
}
[Route("/customers/{Id}", "DELETE")]
public class DeleteCustomer : IReturnVoid
{
public int Id { get; set; }
}
public class Customer
{
[AutoIncrement]
public int Id { get; set; }
public string Name { get; set; }
}
//Provide the implementation for your above Service Contracts
public class CustomerService : Service
{
public object Get(GetCustomers request)
{
return new GetCustomersResponse { Results = Db.Select<Customer>() };
}
public object Get(GetCustomer request)
{
return Db.SingleById<Customer>(request.Id);
}
public object Post(CreateCustomer request)
{
var customer = new Customer { Name = request.Name };
Db.Save(customer);
return customer;
}
public object Put(UpdateCustomer request)
{
var customer = Db.SingleById<Customer>(request.Id);
if (customer == null)
throw HttpError.NotFound("Customer '{0}' does not exist".Fmt(request.Id));
customer.Name = request.Name;
Db.Update(customer);
return customer;
}
public void Delete(DeleteCustomer request)
{
Db.DeleteById<Customer>(request.Id);
}
}
//Write your Integration tests
[TestFixture]
public class CustomerRestExample
{
const string BaseUri = "http://localhost:2000/";
ServiceStackHost appHost;
[TestFixtureSetUp]
public void TestFixtureSetUp()
{
//Start your AppHost on TestFixture SetUp
appHost = new AppHost()
.Init()
.Start(BaseUri);
}
[TestFixtureTearDown]
public void TestFixtureTearDown()
{
//Dispose it on TearDown
appHost.Dispose();
}
/* Write your Integration Tests against the self-host instance */
[Test]
public void Run_Customer_REST_Example()
{
var client = new JsonServiceClient(BaseUri);
//GET /customers
var all = client.Get(new GetCustomers());
Assert.That(all.Results.Count, Is.EqualTo(0));
//POST /customers
var customer = client.Post(new CreateCustomer { Name = "Foo" });
Assert.That(customer.Id, Is.EqualTo(1));
//GET /customer/1
customer = client.Get(new GetCustomer { Id = customer.Id });
Assert.That(customer.Name, Is.EqualTo("Foo"));
//GET /customers
all = client.Get(new GetCustomers());
Assert.That(all.Results.Count, Is.EqualTo(1));
//PUT /customers/1
customer = client.Put(new UpdateCustomer { Id = customer.Id, Name = "Bar" });
Assert.That(customer.Name, Is.EqualTo("Bar"));
//DELETE /customers/1
client.Delete(new DeleteCustomer { Id = customer.Id });
//GET /customers
all = client.Get(new GetCustomers());
Assert.That(all.Results.Count, Is.EqualTo(0));
}
}
- Why ServiceStack?
- Important role of DTOs
- What is a message based web service?
- Advantages of message based web services
- Why remote services should use separate DTOs
-
Getting Started
-
Designing APIs
-
Reference
-
Clients
-
Formats
-
View Engines 4. Razor & Markdown Razor
-
Hosts
-
Security
-
Advanced
- Configuration options
- Access HTTP specific features in services
- Logging
- Serialization/deserialization
- Request/response filters
- Filter attributes
- Concurrency Model
- Built-in profiling
- Form Hijacking Prevention
- Auto-Mapping
- HTTP Utils
- Dump Utils
- Virtual File System
- Config API
- Physical Project Structure
- Modularizing Services
- MVC Integration
- ServiceStack Integration
- Embedded Native Desktop Apps
- Auto Batched Requests
- Versioning
- Multitenancy
-
Caching
-
HTTP Caching 1. CacheResponse Attribute 2. Cache Aware Clients
-
Auto Query
-
AutoQuery Data 1. AutoQuery Memory 2. AutoQuery Service 3. AutoQuery DynamoDB
-
Server Events
-
Service Gateway
-
Encrypted Messaging
-
Plugins
-
Tests
-
ServiceStackVS
-
Other Languages
-
Amazon Web Services
-
Deployment
-
Install 3rd Party Products
-
Use Cases
-
Performance
-
Other Products
-
Future