Skip to content

Commit 6258b8e

Browse files
committed
Improved With class to allow comparison of types from different class
1 parent fb4e98d commit 6258b8e

14 files changed

Lines changed: 181 additions & 53 deletions

File tree

‎Fakes.Contrib/Demo.BookStore.Tests/Demo.BookStore.Tests.csproj‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
<ItemGroup>
5757
<Compile Include="Properties\AssemblyInfo.cs" />
5858
<Compile Include="Repositories\BookRepositoryTest.cs" />
59+
<Compile Include="Services\BookServiceTest.cs" />
5960
</ItemGroup>
6061
<ItemGroup>
6162
<ProjectReference Include="..\Demo.BookStore\Demo.BookStore.csproj">
134 Bytes
Binary file not shown.

‎Fakes.Contrib/Demo.BookStore.Tests/Repositories/BookRepositoryTest.cs‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
using System.Diagnostics;
2-
using System.Linq;
3-
using Demo.BookStore.Contexts;
1+
using Demo.BookStore.Contexts;
42
using Demo.BookStore.Contexts.Fakes;
53
using Demo.BookStore.Models;
64
using Demo.BookStore.Repositories;
75
using Fakes.Contrib;
86
using Microsoft.VisualStudio.TestTools.UnitTesting;
7+
using System.Linq;
98

109
namespace Demo.BookStore.Tests.Repositories
1110
{
@@ -84,7 +83,7 @@ public void InsertShouldInsertBooksWithMatchingTitleInTheContext()
8483
sut.InsertAll(books);
8584

8685
// Assert
87-
context.AssertWasCalled(mock => mock.InsertBooks(With.Array(books).Like((source, book) => book.Title == source.Title)));
86+
context.AssertWasCalled(mock => mock.InsertBooks(With.Enumerable(books).Like<Book>((source, book) => book.Title == source.Title).ToArray()));
8887
}
8988

9089
private static Book[] MakeBooks(int id = 1)
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using Demo.BookStore.Models;
2+
using Demo.BookStore.Repositories;
3+
using Demo.BookStore.Repositories.Fakes;
4+
using Demo.BookStore.Services;
5+
using Demo.BookStore.Services.Dtos;
6+
using Fakes.Contrib;
7+
using Microsoft.VisualStudio.TestTools.UnitTesting;
8+
using System.Linq;
9+
10+
namespace Demo.BookStore.Tests.Services
11+
{
12+
[TestClass]
13+
public class BookServiceTest
14+
{
15+
[TestMethod]
16+
public void InsertBookShouldInsertTheBookInTheRepository()
17+
{
18+
// Arrange
19+
var book = MakeBook();
20+
var repository = new StubIBookRepository().WithObserver();
21+
var sut = MakeSut(repository);
22+
23+
// Act
24+
sut.InsertBook(book);
25+
26+
// Assert
27+
repository.AssertWasCalled(mock => mock.Insert(With<Book>.Like(x => x.Title == book.Title)));
28+
}
29+
30+
[TestMethod]
31+
public void InsertAllBooksShouldInsertTheBooksInTheRepository()
32+
{
33+
// Arrange
34+
var books = MakeBooks();
35+
var repository = new StubIBookRepository().WithObserver();
36+
var sut = MakeSut(repository);
37+
38+
// Act
39+
sut.InsertAllBooks(books);
40+
41+
// Assert
42+
repository.AssertWasCalled(mock => mock.InsertAll(With.Enumerable(books).Like<Book>((dto, book) => dto.Title == book.Title).ToArray()));
43+
}
44+
45+
private static BookDto[] MakeBooks()
46+
{
47+
return new[]
48+
{
49+
MakeBook("Random title 1"),
50+
MakeBook("Random title 3"),
51+
MakeBook("Random title 2")
52+
};
53+
}
54+
55+
private static BookDto MakeBook(string title = "Random title")
56+
{
57+
var book = new BookDto
58+
{
59+
Title = title
60+
};
61+
62+
return book;
63+
}
64+
65+
private static BookService MakeSut(IBookRepository repository = null)
66+
{
67+
repository = repository ?? new StubIBookRepository();
68+
69+
var sut = new BookService(repository);
70+
71+
return sut;
72+
}
73+
}
74+
}

‎Fakes.Contrib/Demo.BookStore/Demo.BookStore.csproj‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
<Compile Include="Repositories\BookRepository.cs" />
4747
<Compile Include="Repositories\IBookRepository.cs" />
4848
<Compile Include="Repositories\IRepository.cs" />
49+
<Compile Include="Services\BookService.cs" />
50+
<Compile Include="Services\Dtos\BookDto.cs" />
4951
</ItemGroup>
5052
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
5153
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using Demo.BookStore.Models;
2+
using Demo.BookStore.Repositories;
3+
using Demo.BookStore.Services.Dtos;
4+
using System;
5+
using System.Linq;
6+
7+
namespace Demo.BookStore.Services
8+
{
9+
public class BookService
10+
{
11+
private readonly IBookRepository _repository;
12+
13+
public BookService(IBookRepository repository)
14+
{
15+
if (repository == null) throw new ArgumentNullException("repository");
16+
_repository = repository;
17+
}
18+
19+
public void InsertBook(BookDto dto)
20+
{
21+
var book = new Book
22+
{
23+
Title = dto.Title
24+
};
25+
26+
_repository.Insert(book);
27+
}
28+
29+
public void InsertAllBooks(BookDto[] dtos)
30+
{
31+
var books = dtos.Select(dto => new Book
32+
{
33+
Title = dto.Title
34+
}).ToArray();
35+
36+
_repository.InsertAll(books);
37+
}
38+
}
39+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+

2+
namespace Demo.BookStore.Services.Dtos
3+
{
4+
public class BookDto
5+
{
6+
public string Title { get; set; }
7+
}
8+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace Fakes.Contrib
6+
{
7+
internal class EnumerableWith<TValue> : IEnumerableWith<TValue>
8+
where TValue : class
9+
{
10+
private readonly IEnumerable<TValue> _source;
11+
12+
public EnumerableWith(IEnumerable<TValue> source)
13+
{
14+
_source = source;
15+
}
16+
17+
public IEnumerable<TOutput> Like<TOutput>(Func<TValue, TOutput, bool> predicate)
18+
where TOutput : class
19+
{
20+
return _source.Select(item => (Func<TOutput, bool>) (x => predicate(item, x))).Select(With<TOutput>.Like);
21+
}
22+
}
23+
}

‎Fakes.Contrib/Fakes.Contrib/Fakes.Contrib.csproj‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,11 @@
5050
<Compile Include="DynamicProxy\Hooks\InterceptEqualsHook.cs" />
5151
<Compile Include="DynamicProxy\Interceptors\CustomEqualsInterceptor.cs" />
5252
<Compile Include="DynamicProxy\Interceptors\EqualsAlwaysTrueIfNotNullInterceptor.cs" />
53+
<Compile Include="EnumerableWith.cs" />
5354
<Compile Include="Extensions\ExpressionExtensions.cs" />
5455
<Compile Include="Extensions\StubObservedCallExtensions.cs" />
5556
<Compile Include="Helpers\AssertHelper.cs" />
57+
<Compile Include="IEnumerableWith.cs" />
5658
<Compile Include="StubBaseExtensions.cs" />
5759
<Compile Include="Properties\AssemblyInfo.cs" />
5860
<Compile Include="With.cs" />

‎Fakes.Contrib/Fakes.Contrib/Fakes.Contrib.nuspec‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@
1111
<iconUrl>http://www.dev-one.com/wp-content/uploads/2013/10/devone-picto-128x121.png</iconUrl>
1212
<requireLicenseAcceptance>false</requireLicenseAcceptance>
1313
<description>$description$</description>
14-
<releaseNotes>Version 0.3: added more flexible ways to verify a mock
15-
Version 0.2: first usable version. Watch out, this is still apha code !</releaseNotes>
14+
<releaseNotes>
15+
Version 0.4: improved With class to allow predicate of two different types
16+
Version 0.3: added more flexible ways to verify a mock
17+
Version 0.2: first usable version. Watch out, this is still apha code !
18+
</releaseNotes>
1619
<copyright>Copyright 2013 Dev One</copyright>
1720
<tags>fakes tdd mocking mocks unittesting agile contribution</tags>
1821
<dependencies>

0 commit comments

Comments
 (0)