Skip to content

Instantly share code, notes, and snippets.

@Vidarls
Forked from marcusoftnet/gist:1222823
Created September 16, 2011 19:11
Show Gist options
  • Save Vidarls/1222865 to your computer and use it in GitHub Desktop.
Save Vidarls/1222865 to your computer and use it in GitHub Desktop.
Base for a repository demo test with Simple.Data.FakeResult
using System;
using System.Collections.Generic;
using FakeItEasy;
using NUnit.Framework;
using Simple.Data.FakeResult;
namespace TestingWithSimpleDataFakeResult
{
[TestFixture]
public class RepositoryUsingSimpleDataTests
{
[Test]
public void should_return_faked_data_when_calling_repository()
{
// Arrange
dynamic fakeResult = FakeResult.For.Users.All().OrderByScore().Take().Returns(GetTestResultUsers());
var dbFactory = A.Fake<ISimpleDataDbFactory>();
A.CallTo(dbFactory).WithReturnType<dynamic>().Returns(fakeResult);
var repositoryUnderTest = new UserRepository(dbFactory);
// Act
var result = repositoryUnderTest.GetTopUsers();
// Assert
Assert.AreEqual(3, result.Count());
}
private IList<User> GetTestResultUsers()
{
return new List<User>
{
new User { Name = "Marcus", Score = 3000},
new User { Name = "Albert", Score = 30000},
new User { Name = "The Gu", Score = 4500},
};
}
}
public class UserRepository
{
private readonly ISimpleDataDbFactory _dbFactory;
public UserRepository(ISimpleDataDbFactory dbFactory)
{
_dbFactory = dbFactory;
}
public IList<User> GetTopUsers()
{
return _dbFactory.DB.Users.All().OrderByScore().Take(3).ToList();
}
}
public interface ISimpleDataDbFactory
{
dynamic DB { get; }
}
public class User
{
public string Name { get; set; }
public int Score { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment