Site: http://ondevelopment.blogspot.com/feeds/posts/default Link: http://ondevelopment.blogspot.com/feeds/posts/default
by Patrik Hägne via On Development on 7/24/2010 11:18:00 AM
This is the seventh and last part in the series of posts where I’m porting Brett Schucherts excelent demo of Mockito in Java to C# and FakeItEasy. The source for this example series can be found in a Mercurial repository at Google code. Each test implementation and following code update is a separate commit so you can easily update your repository to look at the full code at any given state. Find the repository here. Part 1 can be found here. Part 2 can be found here. Part 3 can be found her ...
[ read more ]
by Patrik Hägne via On Development on 6/20/2010 1:28:00 PM
This is the sixth part in the series of posts where I’m porting Brett Schucherts excelent demo of Mockito in Java to C# and FakeItEasy. The source for this example series can be found in a Mercurial repository at Google code. Each test implementation and following code update is a separate commit so you can easily update your repository to look at the full code at any given state. Find the repository here. Part 1 can be found here. Part 2 can be found here. Part 3 can be found here. Part 4 can b ...
by Patrik Hägne via On Development on 5/25/2010 8:42:00 PM
This is the fourth part in the series of posts where I’m porting Brett Schucherts excelent demo of Mockito in Java to C# and FakeItEasy. The source for this example series can be found in a Mercurial repository at Google code. Each test implementation and following code update is a separate commit so you can easily update your repository to look at the full code at any given state. Find the repository here. Part 1 can be found here. Part 2 can be found here. Part 3 can be found here. Test 4 ...
by Patrik Hägne via On Development on 5/23/2010 6:21:00 PM
This is the fourth part in the series of posts where I’m porting Brett Schucherts excelent demo of Mockito in Java to C# and FakeItEasy. The source for this example series can be found in a Mercurial repository at Google code. Each test implementation and following code update is a separate commit so you can easily update your repository to look at the full code at any given state. Find the repository here. Part 1 can be found here. Part 2 can be found here. Test 3 – Not logging in with wrong ...
by Patrik Hägne via On Development on 5/16/2010 1:43:00 PM
This is the fourth part in the series of posts where I’m porting Brett Schucherts excelent demo of Mockito in Java to C# and FakeItEasy. The source for this example series can be found in a Mercurial repository at Google code. Each test implementation and following code update is a separate commit so you can easily update your repository to look at the full code at any given state. Find the repository here. Part 1 can be found here. Test 2 – Revoking accounts User story After three consecutive ...
by Patrik Hägne via On Development on 5/15/2010 12:59:00 PM
This is the first part in a series of blog posts that will demo FakeItEasy through TDD-ing a simple Login Service using C#. This is a port of Brett Schucherts excelent demo of Mockito in Java. As this is a demo of FakeItEasy, I will omit the refactoring part of Brett’s original example, please don’t miss it though as it can be found here. I’ve tried to stay as close to the original example as possible so that they can be easily compared. The source for this example series can be found in a Mercu ...
by Patrik Hägne via On Development on 5/5/2010 9:28:00 PM
Here’s a piece of code that might or might not do what you expect. (It depends on what you expect it to do.)Dim someAge = "999" Dim parsedAge As Integer? = If(someAge = "999", Nothing, Integer.Parse(someAge)) Challenge What will the parsedAge variable contain? Answer It will be a nullable integer with no value, right? Ehh, no wrong! -So, what will it be? Well, it will be a nullable integer – that’s the easy part – but it will have a value, zero (0)! Is this what you expected? It’s not what I ...
by Patrik Hägne via On Development on 2/25/2010 7:36:00 PM
I added a nifty little feature in FakeItEasy today, not 100% sure about the naming though so if you have any ideas pleas tell me. The idea is that when you configure a call you can specify several values and each time a call is made to the configured member (property or method) the next value from the collection will be returned. Here’s what it looks like in action:public interface ICurrentTimeProvider { DateTime CurrentTime { get; } } [Test] public void Example() { var timeProvider = ...
by Patrik Hägne via On Development on 1/6/2010 11:13:00 AM
I use the System.NotImplementedException a lot, whenever I create a new method I have it throwing this until I have let tests drive actual functionality of the method. There is a slight danger in this though (although better than the alternative) in that I might forget to implement a function and this exception creeps into production. The other day I modified the snippet I use so that it outputs pre processor directives, so that the compiler will refuse to compile the code if I forget to impleme ...
by Patrik Hägne via On Development on 12/19/2009 12:40:00 PM
I’ve just updated the way to configure any call to faked objects in FakeItEasy, the syntax is new and also you can now configure return values. Let’s say you have an interface providing localized text resources like this:public interface ILocalizedResources { string SomeText { get; } string SomeOtherText { get; } } When you fake this interface any of the properties would return null when not configured but you might have several tests that are dependant on that the values are non null s ...
by Patrik Hägne via On Development on 11/26/2009 9:06:00 PM
I’ve implemented a new, cleaner simpler, ninja-deluxe-shiny-gold-plated-ultra-cool way of configuring fakes/stubs/mocks in FakeItEasy. The news are two fold: Simpler call specifications Simpler argument validations How’s this for simple?public void Example() { var serviceProvider = A.Fake<IServiceProvider>(); var service = A.Fake<IWidgetFactory>(); A.CallTo(() => serviceProvider.GetService(typeof(IWidgetFactory))).Returns(service); } No need for awkward lambda expr ...
by Patrik Hägne via On Development on 11/7/2009 12:02:00 PM
If there’s a bug in software I’ve created my knee jerk reaction is that I created that bug. The knee jerk reaction of some developers is “there must be a bug in the framework”, which of course turns out to be false 99.9999% of the times. Yesterday I managed to track down a bug that had eluded me for a couple of weeks; an object graph that we’re serializing to the Asp.Net session (using state server) sometimes couldn’t be serialized. Every class that could possibly be contained in the graph was m ...
by Patrik Hägne via On Development on 11/1/2009 12:39:00 PM
In this post I’ll describe a way that lets you specify default configurations for specific types so that any time a fake of that type is created this default configuration will be applied. In FakeItEasy there’s a concept called a fake object container represented by the interface IFakeObjectContainer which has two methods, one for resolving/creating fakes and one for configuring fakes, in this post I’m going to focus on the configuration part.namespace FakeItEasy.Api { using System; usin ...
by Patrik Hägne via On Development on 9/5/2009 4:35:00 PM
FakeItEasy has a scoping feature that lets you configure calls only within a scope or asserting that calls was made within a specific scope. A scope implements IDisposable and the Dispose method closes the scope, this means that to create a scope you create a using statement for it:using (Fake.CreateScope()) { // Do something within the scope... } Any calls configured within a scope are only valid within that scope, this means that you can override the behavior of a faked method for just a ...
by Patrik Hägne via On Development on 9/5/2009 1:32:00 PM
I’ve added a new way to create fake objects in FakeItEasy. You might call the “traditional” way of doing it in FakeItEasy is the “Rhino-style”:public static void TraditionalWay() { var foo = A.Fake<IFoo>(); foo.Configure().CallsTo(x => x.Baz()).Returns(10); int value = foo.Baz(); Fake.Assert(foo).WasCalled(x => x.Baz()); } When creating fakes in this way the returned faked object is of the type specified in the A.Fake-call, faking an IFoo returns an IFoo. When creat ...
by Patrik Hägne via On Development on 8/7/2009 12:15:00 PM
I added a new feature to FakeItEasy the other day, it lets you put a fake wrapper around a real instance. This means that all calls that are not explicitly configured to do anything different are delegated to the wrapped instance, this gives you the ability to fake out only a certain method on a fake. For example, you might have a web service of some sort:public interface IStoreService { IEnumerable<IProduct> FindAllProducts(); int BuyProduct(IProduct product); } public interface ...
by Patrik Hägne via On Development on 7/31/2009 7:22:00 PM
Syntax for event raising is kind of awkward across the line, and for good reason, there simply is no really good way of doing it since an event always has to be positioned to the of an event attachment or detachment (+= or –=). Just from the top of my head I think the way it’s done (or rather one of the ways you can do it) in Rhino Mocks is something like this:var foo = MockRepository.GenerateMock<IFoo>(); foo.Raise(x => x.SomethingHappened += null, foo, EventArgs.Empty); I think (I ha ...
by Patrik Hägne via On Development on 7/30/2009 7:06:00 PM
As I mentioned in my earlier post one of the things I want to avoid in my mock-/stub-/fake-framework is extension methods that clutter intellisense, this means that you have to get a configuration object to be able to start configuring your fake, don’t be afraid though, it’s really easy and there are two ways to do it. The first is to call the static method “Fake.Configure(object fakedObject)” with the fake object you want to configure, but you can also import the namespace “Legend.Fakes.Extensi ...
by Patrik Hägne via On Development on 7/29/2009 11:50:00 PM
OK, you’ll think I’ve lost it – or maybe you never thought I had it – but I’m creating my own mocking framework. Why? Well, first of all because I wanted a framework that is more semantically correct and easier to understand so instead of distinguishing between mocks and stubs it just produces “fakes”, if a fake is a mock or a stub depends on the use of it. I also wanted a simpler and cleaner syntax for configuration, I’m really not a fan of the many extension methods cluttering Intellisense in ...
by Patrik Hägne via On Development on 7/5/2009 10:28:00 PM
A year or so ago I ran into a piece of code that made me a little dizzy and nauseous. I've created a piece of code similar to the code in question but the original code was in VB so I can't truly reproduce it but this will have to suffice. Can you figure out what the code does (line 7 in particular)?public class Foo { private string scoreLabelText; public Foo(decimal score) { score = (Convert.ToInt32((score * 10) / 5) * 5) / 10m; this.scoreLabelText = score.ToString( ...
by Patrik Hägne via On Development on 5/21/2009 10:41:00 PM
There are quite a few nice features in the new 2.5 release of NUnit so if you’ve not yet checked it out please do so. I’ve migrated to it in all our solutions at work and we’ve had no problems, we did have to upgrade to the latest versions for TestDriven.Net and TeamCity, but hey, upgrading is a good thing. One thing I really like is the ValuesAttribute, if you’ve done parameterized tests (that were previously available in the NUnit.Framework.Extensions library) this is something similar. Just t ...
by Patrik Hägne via On Development on 5/17/2009 10:45:00 PM
My first tests of classes are often tests that asserts that the constructor throws ArgumentNullException for null arguments, something like this:[Test] public void constructor_throws_when_fileSystem_is_null() { Assert.Throws<ArgumentNullException>(() => { new TransactionalFileSystem((IFileSystem)null, this.transactionManager); }); } As you see I explicitly cast the null value to the type of the parameter in the constructor signature, this is benefitial ...
by Patrik Hägne via On Development on 4/21/2009 11:41:00 PM
There are a number of different ways to synchronize access to resources in .net, the most common ones are probably the Monitor class (used by the lock-keyword) and the ReaderWriterLock (or ReaderWriterLockSlim) classes, others are semaphores and mutex’s. When using TDD to develop you want to be able to test that your code is actually locking the resources it should but you don’t want to rely on spinning off threads to check if resources are locked or not in your unit tests (because it would actu ...
by Patrik Hägne via On Development on 3/30/2009 12:00:00 AM
I’m a big fan of the Autofac IoC-container, it’s pretty light weight, blazingly fast and very object oriented. One of the features I love the most is the ability to divide the boot strapping code into modules. Also, to set up your dependencies in code with a fluent interface rather than in an xml-file is great in so many ways (intellisense and refactoring to name a few). The one problem I’ve found with this is that it couples your assemblies tighter than they might need to be coupled. The probl ...
by Patrik Hägne via On Development on 2/12/2009 8:03:00 PM
How many times have you written code similar to this? 1: var builder = new StringBuilder(); 2: 3: foreach (string foo in bar) 4: { 5: builder.Append(foo); 6: } 7: ...
by Patrik Hägne via On Development on 1/5/2009 9:03:00 PM
EDIT: The code demonstrated in the article is part of the Legend open source project you can find at http://legend.codeplex.com/. If you've ever created a web control - and God knows I've created a few of them - you've most definitely come in contact with the HtmlTextWriter. If you have you've probably sworn a couple of times over how clunky it is. The amount of code you have to write to create quite simple pieces of html is just insane. For example, take this very basic html tag:<div id="id" ...
by Patrik Hägne via On Development on 10/18/2008 6:20:00 PM
In this post I'll talk about VB's explicit interface implementation and how to best exploit the power this brings. I guess that it's a well known fact that interface implementation in VB is explicit but what does that mean really? It simply means that to implement an interface in a class the developer has to explicitly declare what members of the class implements the members of the interface. C# also has the ability to explicitly implement interfaces but it supports implicit interface implementa ...
by Patrik Hägne via On Development on 5/17/2008 6:13:00 PM
Every now and then I have to create a type converter (System.ComponentModel.TypeConverter), often to convert custom types to and from strings, there is some repetitive work involved in this. You have to override the CanConvertFrom- and CanConvertTo-methods and implement the conversion, now I've done this repetitive work for the last time since I created a class I call TypeConverterBase<TSource, TDestination> which implements all the logic save the conversion which it has two abstract metho ...
by Patrik Hägne via On Development on 3/31/2008 10:57:00 PM
A while back Charlie Calvert and Mads Torgersen wrote about dynamic lookup being part of the plans for C# 4.0. A code block specified with the "dynamic" key word will allow dynamic lookup with syntax like follows: static void Main(string[] args) { dynamic { object myDynamicObject = GetDynamicObject(); myDynamicObject.SomeMethod(); // call a method myDynamicObject.someString = "value"; // Set a field myDynamicObject[0] = 25; ...
by Patrik Hägne via On Development on 2/27/2008 8:59:00 PM
I came up with a little - as far as I'm concerned - cool library consisting of extension methods used to guard input parameters of public methods. It's always a good idea to validate arguments of public methods and throw exceptions if the validation fails. If you require that a parameter is not null, throw an "ArgumentNullException" if the specified argument is null, this makes debugging so much easier and helps other developers that uses your API. To do this in all public methods is ...
by Patrik Hägne via On Development on 2/12/2008 12:21:00 AM
Keyvan Nayyeri has a blog post about exposing generic collections rather than lists in API's. The basis of his post is that he says that it's a bad idea to expose List<T> publicly, I couldn't agree more. Actually I think it's such a bad idea that I didn't realize that it was a wide spread habit. If you're going to expose a collection it should be exposed in one of two ways in my opinion. The best alternative in most cases is a strongly typed collection named after what it contains (fo ...
by Patrik Hägne via On Development on 1/26/2008 9:20:00 PM
If you use the ExpectedExceptionAttribute to assert that an exception is raised in an NUnit-test the type of the thrown exception must match the actual exception's type exactly, for example if you check for an ArgumentException and an ArgumentNullException is thrown the assertion will fail. To me this is wrong since an ArgumentNullException really is an ArgumentException. Of course you could argue that you shouldn't check for general exceptions for the same reasons as you shouldn't catch g ...
by Patrik Hägne via On Development on 1/20/2008 1:38:00 AM
Being a big fan of functional programming languages such as Erlang and Prolog I am very glad to see lambda expressions introduced in C# 3.0 and the VB9. You've been able of writing code in a functional manner since the dawn of C#, but it's been very hard, since version 2.0 it's been easier though with anonymous methods and now with 3.0 and lambda expressions it's truly a walk in the park. One thing I like to do is factoring out common patterns into template functions. A common example of a patte ...
by Patrik Hägne via On Development on 1/8/2008 8:05:00 PM
We had some discussions about my Unit testing pattern-article in one of the forums over at Channel 9. I noticed that a lot of people misread my intentions of the pattern a bit and thought that the advantage of using it is that it enables you to test non public methods, this is however just a side effect (desirable or not) of using the pattern. Therefore I've updated the article to better reflect my intentions. Nested files I also added a neat little trick that shows how to make code files depe ...
by Patrik Hägne via On Development on 1/5/2008 2:46:00 PM
In this article I'm going to present a pattern I've developed for creating xUnit-tests in .Net, code samples will be in C# but the pattern is equally well suited for VB.Net or any other .Net-language that has preprocessor #if-equivalents. The main benefits of using this pattern is that your tests will be an integral part of the code under test and that it gives you access to all non-public members of the tested class making it easier to use test doubles (stubs, mocks, etc.) while also removing ...
The content of the postings is owned by the respective author. CSharpFeeds is not responsible for the contents of the postings. This site is automatically generated and cannot be reviewed for abusive content. If you find abusive content on CSharpFeeds, please contact us. Designated trademarks and brands are the property of their respective owners. All rights reserved.