04/05/2007 - Friday
I've been working on tests for the first bit that I've done for the Healtheriser. I know I should have done them first but I had never really got into the swing of it. Over the last week I've been reading a book that does a website using the TDD (Test Driven Development) technique and it allowed me to start to get my head around it. I'm also now starting to understand integration tests, which is like simulating a user doing various actions on the website one after the other (like logging in, viewing a page, adding something, editing it, viewing it, deleting it, adding something else, then logging out), which is different from the other tests that only tested each individual part and not how they fitted together.
I ran into one problem with writing the integration tests though. I'm using OpenID for logging in and that involves another website to authenticate you are who you claim you are. This doesn't really work well with integration tests as they're not running as a real website so the OpenID website can't contact back saying who I actually am. In my other tests I could easily fake that I was logged in as I have access to the session variable where I can store the user, but in the integration tests I don't have write access to that variable, I can only read it, so that way would fail.
I did a quick google search and could only find one solution. It requires mocking a few of the OpenID library methods to return what I want them to return instead of it contacting the real OpenID website. It still does do a bit of OpenID work as I've no idea how to mock the begin method of the OpenID checker, which attempts to find out what it should do with the given openid url. I think that's the only activity it does and it doesn't actually contact the OpenID website for the openid url, but I'm not entirely sure. I'll see if I can edit it later so that it doesn't do any checking at all.
Anyway, he's the code that I used to mock out most of the OpenID stuff. I have it in a seperate method in my testing DSL as I use it in two different places (not just a single login method). It expects to be passed a model object which contains at least a variable called openid_url. It also does some of the optional OpenID values, but that can be deleted out if you don't need them. You can also easily change the name of the openid url variable, it's only used on the last line of the method.
def stub_user(user)
OpenID::Consumer.any_instance.stubs(:complete).returns(
OpenID::SuccessResponse.new(1, {}))
OpenID::SuccessResponse.any_instance.stubs(:status).returns(
OpenID::SUCCESS)
OpenID::SuccessResponse.any_instance.stubs(
:extension_response).returns(HashWithIndifferentAccess.new(
:nickname => user.nickname, :email => user.email,
:gender => user.gender))
OpenID::SuccessResponse.any_instance.stubs(
:identity_url).returns(user.openid_url)
end
Just call the
stub_user method before doing a get request to the login page, then the post request to complete the login.

0 comments:
Post a Comment