TDD: Testen ob eine Cookie gesetzt wurde

by robert 14. May 2009 16:14

Für Webentwickler ist das Auswerten und Setzen von Cookies Business-Logik und Business Logik sollte Testgetrieben entwickelt werden.

Hier ein Beispiel Unit-Test:

Response _response { get { return _httpCurrent.Response as ResponseNoWeb; } }
RequestNoWeb _request { get { return _httpCurrent.Request as RequestNoWeb; } }
 
[Test]
public void SetCookie()
{
    _request.Url = new Uri("http://de.camping.info");
 
    _uiLanguageService.SetLanguageCookie("de");
    
    Assert.That(_response.CookiesCreated[0].Value, Is.EqualTo("de"));
    Assert.That(_response.CookiesCreated[0].Name, Is.EqualTo("Lang"));
    Assert.That(_response.CookiesCreated[0].Domain, Is.EqualTo(".camping.info"));
}
 
 
Ermöglicht wird das über die Abstrakton von HttpRequest und HttpResponse. (Die Entwicklung wurde gestern begonnen, der Quelltext kapselt daher nur das Grundlegenste.)
 
Erfolgt die Anwendung nicht im Webkontext verwenden wir eine Implementierung für den lokalen Gebrauch:
 
namespace SpeakFriend.Utilities.Web
{
    public class ResponseNoWeb : Response, IResponse
    {
        public void Redirect(string url)
        {
            _redirections.Add(url);
        }
 
        public void Redirect(string url, bool endResponse)
        {
            _redirections.Add(url);
        }
 
        public void SetCookie(HttpCookie cookie)
        {
            _cookiesCreated.Add(cookie);
        }
    }
}

…rudimentär, aber wirksam.

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

About Oliver

shades-of-orange.com code blog logo I build web applications using ASP.NET and have a passion for javascript. Enjoy MVC 4 and Orchard CMS, and I do TDD whenever I can. I like clean code. Love to spend time with my wife and our children. My profile on Stack Exchange, a network of free, community-driven Q&A sites

About Anton

shades-of-orange.com code blog logo I'm a software developer at teamaton. I code in C# and work with MVC, Orchard, SpecFlow, Coypu and NHibernate. I enjoy beach volleyball, board games and Coke.