CSharpFeeds - All your C# feeds in one place.

Sponsors

Feed: Legend and truth

Site: http://ondevelopment.blogspot.com/feeds/posts/default Link: http://ondevelopment.blogspot.com/feeds/posts/default

Tuesday, December 06, 2011

Undefined… uhhh… defined

by Patrik Hägne via Legend and truth on 12/6/2011 10:59:00 AM

Yes, we all know it, Javascript has some serious flaws. One of the more disturbing one being that undefined can be defined:// This is way legal, but way weird: undefined = “defined”; Of course, it’s easy enough to avoid this, just don’t f-ing do it. But when writing scripts we are often playing in the same sandbox as the “big bad bullying, just straight up evil kids that has nothing better to do than going around defining undefined” are playing in. That’s why it’s very common to see scripts test ...

[ read more ]

Sunday, April 10, 2011

An alternative to NotImplementedException

by Patrik Hägne via Legend and truth on 4/10/2011 10:51:00 AM

It happens from time to time that I forget to implement some code where I’m just throwing a NotImplementedException. Being forgetful I have a habit of coming up with stuff that will help me not to forget. I think my “MustBeImplementedException” is quite useful, it will break the build if the build is not DEBUG.#if DEBUG namespace FakeItEasy { using System; using System.Diagnostics.CodeAnalysis; /// <summary> /// An exception that can be thrown before a member has been ...

[ read more ]

Saturday, April 02, 2011

An old pain point removed from FakeItEasy

by Patrik Hägne via Legend and truth on 4/2/2011 1:37:00 PM

It’s always bittersweet to say that you have introduced a breaking change in an API that people use. Bitter because it will break peoples code. But sweet because IT’S AN AWESOME NEW FEATURE! I was never really fond of the fact that argument constraints relies on implicit casting in FakeItEasy, this is why you had to write A<IFormattable>.Ignored.Argument but you could write the shorter A<string>.Ignored for non interface types. This had to be done in order for the chaining of argumen ...

[ read more ]

Saturday, February 05, 2011

Experimental branches in Mercurial

by Patrik Hägne via Legend and truth on 2/5/2011 3:30:00 PM

Branching in Mercurial is easy, it’s so easy that most of the times you’re branching you don’t even know that you’re doing it. Have you ever had to merge? I guess you have, well guess what, when you’re merging, you’re merging two branches! Ever got an error when pushing stating that a push would create two heads in the remote repository? Head == branch. I had a conversation the other day with a fellow developer about Git, I asked him what the main benefits of Git was. The thing is I’m pretty muc ...

[ read more ]

Monday, October 04, 2010

Fixture initialization in FakeItEasy

by Patrik Hägne via Legend and truth on 10/4/2010 10:31:00 PM

I added a new feature to FakeItEasy that makes it – you guessed it – easy to initialize test fixtures with fakes. Instead of calling A.Fake<WhateverTypeYourFakingHere>() once for every fake you have in your test fixture you can simply tag the variables or properties you want to be fakes with the “FakeAttribute” and then call the method “Fake.InitializeFixture(this)” in your fixture setup:[TestFixture] public class InitializeFixtureIntegrationTests { [Fake] IFormatProvider formatProvide ...

[ read more ]

Sunday, September 26, 2010

Extending exception messages in FakeItEasy

by Patrik Hägne via Legend and truth on 9/26/2010 9:03:00 PM

One of the main features of FakeItEasy is the informative exception messages, let’s say we have the following types:public interface IPersonRepository { void Save(Person personToSave); } public class Person { public string FirstName { get; set; } public string LastName { get; set; } public DateTime DateOfBirth { get; set; } } Now, let’s create a test that shows us what the exception messages in fake it easy looks like when asserting:public void Extending_exception_messages() ...

[ read more ]

Saturday, July 24, 2010

FakeItEasy Login Service Example Series – Part 7

by Patrik Hägne via Legend and truth 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 ]

Sunday, June 20, 2010

FakeItEasy Login Service Example Series – Part 6

by Patrik Hägne via Legend and truth 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 ...

[ read more ]

Tuesday, May 25, 2010

FakeItEasy Login Service Example Series – Part 4

by Patrik Hägne via Legend and truth 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 ...

[ read more ]

Sunday, May 23, 2010

FakeItEasy Login Service Example Series – Part 3

by Patrik Hägne via Legend and truth 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 ...

[ read more ]

Sunday, May 16, 2010

FakeItEasy Login Service Example Series – Part 2

by Patrik Hägne via Legend and truth 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 ...

[ read more ]

Saturday, May 15, 2010

FakeItEasy Login Service Example Series – Part 1

by Patrik Hägne via Legend and truth 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 ...

[ read more ]

Wednesday, May 05, 2010

You don’t understand everything until you understand Nothing

by Patrik Hägne via Legend and truth 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 ...

[ read more ]

Thursday, February 25, 2010

Return from sequence in FakeItEasy

by Patrik Hägne via Legend and truth 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 = ...

[ read more ]

Wednesday, January 06, 2010

Not implemented? Implemented.

by Patrik Hägne via Legend and truth 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 ...

[ read more ]

Saturday, December 19, 2009

Configuring any call to an object

by Patrik Hägne via Legend and truth 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 ...

[ read more ]

Thursday, November 26, 2009

New configuration syntax for FakeItEasy

by Patrik Hägne via Legend and truth 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 ...

[ read more ]

Saturday, November 07, 2009

Fix that bug will ya? NO!

by Patrik Hägne via Legend and truth 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 ...

[ read more ]

Sunday, November 01, 2009

Configuring fake objects on a global scale

by Patrik Hägne via Legend and truth 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 ...

[ read more ]

Saturday, September 05, 2009

Fake scopes explained

by Patrik Hägne via Legend and truth 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 ...

[ read more ]

The other way of faking it

by Patrik Hägne via Legend and truth 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 ...

[ read more ]

Friday, August 07, 2009

Fake wrappers (candy inside)

by Patrik Hägne via Legend and truth 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 ...

[ read more ]

Friday, July 31, 2009

Event raising syntax

by Patrik Hägne via Legend and truth 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 ...

[ read more ]

Thursday, July 30, 2009

Legend.Fakes configuration fluent interface

by Patrik Hägne via Legend and truth 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 ...

[ read more ]

Wednesday, July 29, 2009

Introducing Legend.Fakes

by Patrik Hägne via Legend and truth 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 ...

[ read more ]

Sunday, July 05, 2009

Round and round (my head spins)

by Patrik Hägne via Legend and truth 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( ...

[ read more ]

Thursday, May 21, 2009

NUnit ValuesAttribute is tasty delicious!

by Patrik Hägne via Legend and truth 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 ...

[ read more ]

Sunday, May 17, 2009

Cast null values in tests

by Patrik Hägne via Legend and truth 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 ...

[ read more ]

Tuesday, April 21, 2009

Synchronization and unit testing

by Patrik Hägne via Legend and truth 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 ...

[ read more ]

Monday, March 30, 2009

Unleashing modules

by Patrik Hägne via Legend and truth 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 ...

[ read more ]

Thursday, February 12, 2009

String concatenation made easy

by Patrik Hägne via Legend and truth 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:  ...

[ read more ]

Monday, January 05, 2009

HtmlTextWriter fluent interface

by Patrik Hägne via Legend and truth 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" ...

[ read more ]

Saturday, October 18, 2008

Implementing interfaces in VB.Net

by Patrik Hägne via Legend and truth 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 ...

[ read more ]

Saturday, May 17, 2008

For the last time

by Patrik Hägne via Legend and truth 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 ...

[ read more ]

Monday, March 31, 2008

Dynamic lookup

by Patrik Hägne via Legend and truth 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; ...

[ read more ]

Wednesday, February 27, 2008

Guarding with extension methods

by Patrik Hägne via Legend and truth 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 ...

[ read more ]

Tuesday, February 12, 2008

Exposing collections

by Patrik Hägne via Legend and truth 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 ...

[ read more ]

Saturday, January 26, 2008

Expected exception

by Patrik Hägne via Legend and truth 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 ...

[ read more ]

Sunday, January 20, 2008

Template functions

by Patrik Hägne via Legend and truth 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 ...

[ read more ]

Tuesday, January 08, 2008

Unit testing discussions and nested code files

by Patrik Hägne via Legend and truth 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 ...

[ read more ]

Saturday, January 05, 2008

Unit testing pattern

by Patrik Hägne via Legend and truth 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 ...

[ read more ]

Subscribe

New Feed

Product Spotlight

Recently Updated Sources

Legal Note

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.

Advertise with us