grzesiek-galezowski / tdd-ebook

Test-Driven Development - Extensive Tutorial. Open Source ebook
https://leanpub.com/tdd-ebook
Other
361 stars 57 forks source link

AutoFixture Any class #8

Closed mwhelan closed 11 years ago

mwhelan commented 11 years ago

Hi @grzesiek-galezowski

I'm really enjoying your TDD ebook. Thanks for putting all the time into it. I'm interested in the way that you wrap AutoFixture in an Any class, and I've seen you do an Except method on Any too. I would love to see a full implementation of this class. Do you have it online somewhere, or could you post/email one?

Thanks Michael

grzesiek-galezowski commented 11 years ago

Hi, actually, I wrote one implementation at work, but I cannot post it publicly because of company copyrights etc.. I'm trying to reproduce it from scratch as a side project at home and when I finish, I'd like to make it an addition to the tutorial, however, I'm quite busy lately (preparing for some internal conferences, writing a book, reading books, participating a little in Pickles) and I always have to choose - either write a blog post or learn something or write another chapter (thankfully, this could be partially linked to the book) so it has to wait.

That said, most of the methods I have in the Any type are quite straightforward. Most are just calling AutoFixture methods for specific type, Like Any.Integer(). To implement an Except method, you just: 1) for enum - use Enum.GetValues() to get an array of all values. Then remove the items you don't want and pick a random item from what's left.I remember that in the meantime, the array from GetValues() has to be converted to something else, since it's lacking in its interface. 2) For other types, like int, I keep getting Any.Integer() until I get something that's not on the list of forbidden values.

The most complex part of my implementation of Any is Any.InstanceOf(). When I wrote it, we had some serious legacy code where objects with a lot of getters were injected as interfaces and mocking them was very troublesome. Also, AutoFixture does not support creating fake objects for interfaces and abstract classes out of the box and mocking frameworks, on the other side, make objects that return some "null/default" values from their properties and methods, so they're not as good for "don't care values". Thus, I have my own implementation using Castle.Proxy itself. Anyway, for starters, you may just return a mock from Any.InstanceOf().

Until I finish my homegrown implementation (let's say that I'll make it the criteria for closing this issue), here's the list of methods my company-grown implementation of Any currently supports:

string String()
string StringMatching(string regex)
string StringOfLength(int charactersCount)
string StringOtherThan(params string[] alreadyUsedStrings)
string StringNotContaining(params string[] substrings)
IEnumerable<T> EnumerableOfDerivativesFrom<T>() where T : class
IEnumerable<T> Enumerable<T>()
IEnumerable<T> EnumerableWithout<T>(IEnumerable<T> excluded) where T : class
T[] Array<T>()
T[] ArrayWithout<T>(IEnumerable<T> excluded) where T : class
List<T> List<T>()
ISet<T> Set<T>()
Dictionary<T, U> Dictionary<T, U>()
IEnumerable<T> EnumerableSortedDescending<T>()
string LegalXmlTagName()
char AlphaChar()
char DigitChar()
int Integer()
int IntegerOtherThan(params int[] others)
byte Byte()
byte ByteOtherThan(byte other)
double Double()
bool Boolean()
object Object()
T Exploding<T>() where T : class
Type Type()
T InstanceOf<T>() where T : class
T ValueOf<T>()
string Identifier()
Uri Uri()
string UrlString()
T Of<T>() //for enums
T Of<T>(params T[] values) //for enums
T Besides<T>(params T[] excludedValues) //for enums
T OtherThan<T>(params T[] excludedValues) where T : class
DateTime DateTime()
TimeSpan TimeSpan()
int Port()
string Ip()
string AlphaString()
string AlphaString(int maxLength)

By the way, if you looked at AutoFixture, you may have noticed that it's immensely powerful and configurable, so the Any type actually strips some of its functionality and closes it behind more "primitive" interface :-).

Thanks for taking the time to read my tutorial, I hope you like the future chapters and will give me some more feedback in the future! :-)

grzesiek-galezowski commented 11 years ago

By the way, any contribution is welcome. It's open source project after all :-).

mwhelan commented 11 years ago

Yeah, I'd be interested to talk about that offline. You can reach me at mjmwdev@gmail.com

On Mon, Aug 26, 2013 at 1:35 PM, Grzegorz Gałęzowski < notifications@github.com> wrote:

By the way, any contribution is welcome. It's open source project after all :-).

— Reply to this email directly or view it on GitHubhttps://github.com/grzesiek-galezowski/tdd-ebook/issues/8#issuecomment-23259397 .

grzesiek-galezowski commented 11 years ago

Created a project: https://github.com/grzesiek-galezowski/tdd-toolkit It should be quite usable as of now. Any further issues regarding Any will be logged there.

mwhelan commented 11 years ago

That's awesome @grzesiek-galezowski . Look forward to checking it out.