Skip to content

Instantly share code, notes, and snippets.

@marcusoftnet
Forked from Vidarls/gist:1222865
Created September 16, 2011 19:41
Show Gist options
  • Save marcusoftnet/1222941 to your computer and use it in GitHub Desktop.
Save marcusoftnet/1222941 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>();
// This next line runs into problem since dynamic doesn't play nicely with extensions methods
//
/*
*Error 1 'FakeItEasy.Configuration.IAnyCallConfigurationWithReturnTypeSpecified<System.Collections.Generic.IList<TestingWithSimpleDataFakeResult.User>>'
*has no applicable method named 'Returns' but appears to have an extension method by that name.
*Extension methods cannot be dynamically dispatched.
*Consider casting the dynamic arguments or calling the extension method without the extension method syntax.
*/
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