Tuesday, December 29, 2009

Unit testing losse flarden

Wanneer je start met unit testing, zijn er diverse handvaten die je kunt gebruiken om unit testing te gebruiken in je applicaties.
In de test logica mag je geen gebruik maken van
- IFs statements
- Switch of case statements

- Vervolgens moet de test code die je opstelt herhaalbaar uitgevoerd kunnen worden.
- Vermijd Multiple Asserts.
- Integration test en een unit test moet je scheiden
Alle testen in de solution runnen, wanneer een test failed dan is er een probleem. Dus -> betere test
-> integration test
-> er is geen configuratie maar je moet de test vertrouwen
Een verschil tussen een unit test en integration test is dat bij een integration test configuration noodzakelijk is. (gebruik de postfix unittest en integrationtest voor de desbetreffende testen)

Bij 'gewone' code geldt al gebruik GEEN magic numbers, en dit gaat ook op voor test code.

Syntax method names (bij testen
- method name
-state undertest
- expected behavior
-> dat leidt tot 'Add_LessThanZero_ThrowException'.

Helper methods
- '_make' -> create
- '_change' -> update
- '_assert_'

- Alleen public testen. DIt is een contract naar buiten toe. Private/protected is voor intern gebruik en meer verandelijk.
- Incremental work! (stapje voor stapje)
- [setup] attribut, dit is uitgevoerd voor ELKE test
- start een fail test, de test mag niet blijven 'falen' als de productie code is aangepast
- TDD -> vind de bugs sneller
- Simple code kan bugs bevatten
- Test names zijn lang maar dit is readabily (documentatie)
- Duplicated de logic in de test is een probleem, je test de logica niet. Het eindresultaat is niet gecontroleerd de bug wordt gewoon opnieuw gecreereerd in de Assert.AreEqual. Unittest mag geen logica bevatten, zelfs geen string.Format etc.
- Verander nooit een test. Als een test niet goed is (failuing test). Tegenstrijdige requirements vraag na welke requirement geldig is.
- Code coverage

Saturday, November 7, 2009

SQL datetime headaches

I'm always hassleing with datetimes in the SQL Server database to get the correct datetime format that i want. It seems that most of this stuff you can do with specifying a format when converting it:
Usefull page for that

But it did not realy have what i wanted from it.

So the final solution was the following
to get a format like this:
2008-11-13 17:29:00

I needed to do the following

SET generated = cast( getdate() - 8 as smalldatetime)

WCF service error contract name not found

I had a stupid error a few weeks back which involved a WCF service, A end of the day configuration issue and some totaly unrelated error message. In short after googleing i came across the solution and i decided it was one to remember.

solution on the blog of :

The contract name xxxxxxx could not be found in the list of contracts implemented by the service

So what you need to do:
Check names of interfaces
Check namespaces
Check the service attributes (
[AspNetCompatibilityRequirements(RequirementsMode=AspNetCompatibilityRequirementsMode.Allowed)])

What is Inversion of control and Dependency injection

recently we started to use Unity for projects. Unity is a way to facilitate dependency injection into you code.

And we where using some of the terms often related to this topic and it was confusing me. I decided to get my facts straight.

Inversion Of Control
Inversion of control article that i have found usefull
This article states that inversion of control is a priciple.
a short statement that signifies the base principle:
"Don't call us, we will call you"

the principle comes down to the following:
A Control gets all his data and events from the outside and itself does not know anything about the context it is being used in. This results in lousely coupled Code/controls which in turn should make them more reusable.
Dependency Injection
Dependency injection is a way to achieve Inversion of control

http://www.codeproject.com/KB/aspnet/IOCDI.aspx

Conlusion

Inversion of control is a principle with the goal to help you create better, more reusable code

Dependency injection is a specific method of achieving inversion of control.(Unity is a method for dependency injection)

Path resolution through IUrlResolutionService

When developing for a web application you often come accross the scenario where you need to generate a path to a different page and all you have is a app relative path. To resolve this path to a usable client path i have often used different methods to resolve a path.

When working on a project at work i saw a colleage use the following interface
IUrlResolutionService

I had never seen that one before and i looked it up
MSDN page

Quote from the page:
Defines a service implemented by objects to resolve relative URLs based on contextual information.
....

This interface defines the service that is used to resolve URL paths. The IUrlResolutionService interface is used for resolving relative paths and paths that contain the ~ operator. Server controls that reference resources can define the paths to the resources through the ~ operator, which represents the root of the current application. A path containing the ~ operator will not work if passed to the browser. The server control must convert the path to an absolute or relative path before passing it to the browser.

Control implements this interface. A control that derives from Control could override this implementation to provide customized resolution of URLs.

This interface is implemented by the Control, among other classes, which means that Controls like Page, etc all have a method to resolve the path.
you can pass along controls or the page deeper into your code using this interface to resolve app relative paths for you. For example "~/foo/bar.aspx" would get resolved properly by the control to a path the client can use based on the current context of the control/page.

I myself are using this more often these days it seems a nice way to do it.

Steps until failure

This is a piece of code i came accross that i thought that was interesting. I came accross this piece of code and i thought i should remember it for future reference

on Found on the website:
http://odetocode.com/Blogs/scott/default.aspx

void Process(){

Func<bool>[] steps = {
Step1,
Step2,
Step3,
Step4,
Step5
};

ExecuteStepsUntilFirstFailure(steps);

}

void ExecuteStepsUntilFirstFailure(IEnumerable<Func<bool>> steps)
{
steps.All(step => step() == true);
}

The All operator is documented as stopping as soon as a result can be determined, so the above code is equivalent to the following:

void ExecuteStepsUntilFirstFailure(IEnumerable<Func<bool>> steps)
{
foreach (var step in steps)
{
if (step() == false)
{
break;
}
}
}


Thursday, October 29, 2009

Environment.NewLine

When you create a string with must have multiple line use the option Environment.NewLine and not the escape characters.
see