This project is read-only.
First things first
Quick Start Guide

Setup
Adding IslandUnit in your test project
How to configure IslandUnit

Creating mocks and stubs
Creating stubs with automatically generated data
Creating stubs with manually generated data
Creating stubs with automatically and manually generated data
Creating mocks to methods that should not be called
Creating mocks to methods that should be called n times
Chaining stubs and mocks creation

Using the isolated dependency
Getting the set up dependency
Putting the set up dependency in IoC container

Setup

Adding IslandUnit in your test project
Easy way: Download and add a reference to the IslandUnit assembly (IslandUnit.dll), and add references to Moq, NBuilder and to one IoC container (See what frameworks we already support) in your test project.
Easier way: You can use Nuget to add the IslandUnit in your test project (wich will add Moq and NBuilder automatically) and to one IoC container.

How to configure IslandUnit
You don't have to do any configuration!
You just have to add IslandUnit in your test project and enjoy.
The discovery process will automatically search the IoC container, and if not find, you will be informed.

Creating mocks and stubs

In the following code snippets, we'll suppose that we want to test something (for example, an Controller Action, in ASP.NET MVC), which have an dependency to the interface IProductRepository:

public interface IProductRepository
{
   IEnumerable<Product> GetAll();
   Product GetOne(int productId);
   void InsertOrUpdate(Product product);
   void Save();
}

public class Product
{
   public int ProductId { get; set; }
   public string Name { get; set; }
   public string Description { get; set; }
}

public class ProductController : Controller
{
   private readonly IProductRepository productRepository;

   [Inject]
   public ProductController(IProductRepository productRepository)
   {
      this.productRepository = productRepository;
   }

   public ViewResult Index()
   {
      IEnumerable<Product> products = this.productRepository.GetAll();

      return View(products);
   }

   public ViewResult Details(int id)
   {
      Product product = this.productRepository.GetOne(id);

      return View(product);
   }
}

Go to the Quick Start Guide to see a more complete example.

Creating stubs with automatically generated data
var isolator = new Isolator();

isolator.AddDependency<IProductRepository>()
                .WithStubFor(m => m.GetAll()).ReturningWith(5); // This line creates the stub returning 5 itens

// Ask the IslandUnit to create a Controller instance
// populated with all its dependencies
var sut = isolator.CreateSut<ProductController>();

// Invoke Controller Action
ViewResult viewResult = sut.Index();
Then, when the "Index" Action Controller does that:

IEnumerable<Product> products = this.productRepository.GetAll();
The products variable will have 5 itens, created automatically, containing those values:

ProductId Name Description
1 Name1 Description1
2 Name2 Description2
3 Name3 Description3
4 Name4 Description4
5 Name5 Description5

Or even that:

var isolator = new Isolator();

isolator.AddDependency<IProductRepository>
                .WithStubFor(m => m.GetOne(1)).ReturningOne(); // This line creates the stub returning 1 item

// Ask the IslandUnit to create a Controller instance
// populated with all its dependencies
var sut = isolator.CreateSut<ProductController>();

// Invoke Controller Action
ViewResult viewResult = sut.Details(1);
Then, when the "Details" Action Controller does that:

Product product = this.productRepository.GetOne(id);
The product variable will have 1 item, created automatically, containing those values:

ProductId Name Description
1 Name1 Description1


Creating stubs with manually generated data
var isolator = new Isolator();
var myProduct = new Product() { ProductId = 1000, Name = "MyName", Description = "MyDescription" };

isolator.AddDependency<IProductRepository>
                .WithStubFor(m => m.GetOne(1)).Returning(myProduct);

// Ask the IslandUnit to create a Controller instance
// populated with all its dependencies
var sut = isolator.CreateSut<ProductController>();

// Invoke Controller Action
ViewResult viewResult = sut.Details(1);
Then, when the "Details" Action Controller does that:

Product product = this.productRepository.GetOne(id);
The product variable will have 1 item containing those values:

ProductId Name Description
1 MyName MyDescription


Creating stubs with automatically and manually generated data
var isolator = new Isolator();
var myProduct = new Product() { ProductId = 1000, Name = "MyName", Description = "MyDescription" };

isolator.AddDependency<IProductRepository>()
                .WithStubFor(m => m.GetAll()).ReturningWith(5).And() // This line creates the stub returning 5 itens automatically
                .WithStubFor(m => m.GetOne(1)).Returning(myProduct); // This line creates the stub returning 1 item created manually

// Ask the IslandUnit to create a Controller instance
// populated with all its dependencies
var sut = isolator.CreateSut<ProductController>();

// Invoke Controller Actions
ViewResult viewResult1 = sut.Index();
ViewResult viewResult2 = sut.Details(1);

Creating mocks to methods that should not be called
ToDo

Creating mocks to methods that should be called n times
ToDo

Chaining stubs and mocks creation
ToDo

Using the isolated dependency

Getting the set up dependency
ToDo

Last edited Mar 18, 2011 at 11:57 PM by andborges, version 18

Comments

No comments yet.