Posts

Unit tests vs integration tests

Great article on the difference and on why both are important.

Write your own blog engine, part 6

Adding features What new features can I add to this to make it more useful? Paging is one that would be immediately useful (right now I will return the most recent five posts but I have no way of getting to older ones). Comments are also necessary; I will keep them in mind for later. There are two aspects of paging: I need to return a list of links pointing to pages and I need to be able to display that page when I click on the link. A problem comes to mind: since the posts are sorted in descending order based on their date, each time I add a new post the content of all pages will change. I don't know how this affects something like search engines; you might want to use paging based on the month of the post instead. I'll start with the first part, the navigation links. I will return those using the new HTML5 nav tag: <nav> <ul> <li><a href="/Home/Index/1">1</a></li> <li><a href="/Home/Index/2">2</a...

Smoking causes cancer

Most people are idiots. You can easily prove this because they will agree with the claim in the title. If you try to give them a counterexample — like, you know, the vast majority of smokers not having cancer — they will counter it by "well, it doesn't cause cancer right away". Causality doesn't work like that; it's a "if X, then Y" relationship. Don't use the word if you mean something other than that; say "smoking increases the probability of getting cancer" (yes it does). As an additional counterexample to the claim, here's something most people will dis agree with: passing under a ladder brings bad luck. Mark Twain had a humorous take on this: a character tells another "X passed under a ladder and only six months later he fell and broke his arm" (I'm quoting from memory). That's exactly the same type of "causality" as in the case of smoking: you do X, then long after that bad thing Y happens, and you cl...

World history History of the human race

Here's the official version: In the first ten thousand years, people didn't do much of anything. In the second ten thousand years, people didn't do much of anything. In the third ten thousand years, people didn't do much of anything. In the fourth ten thousand years, people didn't do much of anything. ... repeat for several million years ... Finally, in the last ten thousand years people have started spreading out and inventing cars, airplanes and other interesting stuff.

Write your own blog engine, part 5

Post management I have decided to change the scope of this chapter: it won't be limited just to deleting but instead it would be about general post management: identifying, adding, deleting and modifying posts. I will use the value of the Id field to identify posts; that means that an URL like /Home/Post/guid would take me to a specific post. However, before I start to go in that direction I realize that it makes more sense to create a new PostController class to handle everything about posts. That way, the URL for adding them would be /Post/Add and the one for displaying a single post would be /Post/Details/guid. That means changing a lot of tests; I verify that they're all green, including the one that I disabled earlier (they are) and then start searching for the AddPost string in the tests. The first one is in the acceptance test I just re-enabled so I'll change "Home/AddPost" to "Post/Add". It is now failing with a (404) Not Found. error. ...

Write your own blog engine, part 4

Adding posts I finally arrive at the "raison d'ĂȘtre" of a blog: adding posts. I want to be able to add posts and have them be returned in the next GET call to the home page. I also want to be able to add posts out of order by explicitly indicating the date/time of the new post (so that it gets sorted somewhere else than the top position). I'll start with an acceptance test for the first part: [TestMethod] public void AddingAPostReturnsItOnTheHomePage() { var guid = Guid.NewGuid(); var POST_DATA = string.Format("Title={0}&Content=This is a test", guid); var cookie = Post("/Account/LogOn", "Username=user&Password=pass"); Post("/Home/AddPost", POST_DATA, cookie); Get(); var articles = root.SelectNodes("/html/body/article").ToList(); Assert.IsTrue(articles.Any()); var topArticle = articles.First(); var header = topArticle.SelectSingleNode(...

Write your own blog engine, part 3

Authenticating the user I can't let everyone add posts to my blog; that means having a method to distinguish between a visitor and the owner of the blog. One way to do that would be to check the IP of the incoming connection and verify that it belongs to a list of known IPs; however, that would limit me to a fixed number of computers. What if I'm visiting someone and come up with a new idea for a blog post? No, the correct way of handling this is to add a login screen and verify that the user and password are correct. I am going to use forms authentication so the result of the login process would be a cookie that would re-authenticate the user on subsequent page views. The process will be: user hits an URL that requires authentication user gets redirected to the /Account/LogOn page (and the previous URL gets saved) user enters credentials and clicks the Submit button the credentials are verified; if they are not valid, the user gets redirected to the home page if the cr...