Saturday, December 31, 2005

Testing Rails - Posting to another controller

I've spent a fair chunk of time this morning trying to get my functional tests on my rails app working with the login component. Here's the result of my time, presented in the hopes that it will help someone else who's stuck on the same thing.

I'm using ModelSecurity for the user login component because I'd also like to use the model security bits. Since I've got a before_filter to verify a user is logged in in my controller, I need to log in the user before I can test anything. Logging in the user is easy, right - I just post to the login action of the user controller. Here it is, wrapped in a function because I'll have to call it multiple times:

  def login_Neo
    post :login, :user=>{:login=>users(:neo).login, :password=>NEO_PASSWORD}
  end

  def test_list
    login_Neo
    get :list
    assert_response :success
    assert_template 'list'
    assert_not_nil assigns(:recipes)
  end

Ok, I'm sure that I've got a problem because my controller doesn't have a login action, but I decided to run it and see what happens.

Expected response to be a <:success>, but was <302>

Yep, I'm being redirected to the user controller because of my before_filter.

Now, I know that I need to make it post to the user controller, not the recipedex controller. Much googling, reading and thinking later, I discover that the post method being used here is defined in ActionPack from Rails. And, once I dig up the source for that, I discovered that post simply uses the @controller instance variable.

Aha! One simple change and I'm on my way:

  def login_Neo
    controller_bak = @controller
    @controller = UserController.new
    post :login, :user=>{:login=>users(:neo).login, :password=>NEO_PASSWORD}
    @controller = controller_bak
  end

Now, it posts where I want it to, sets User.current as I expect and everything works!

Wednesday, December 14, 2005

A Feast For Crows

I (well, Amber really) picked up the latest George R R Martin book at the library today. It's on one week checkout. Hmm...685 pages in one week with a baby due anytime. I wonder if I'll finish it.

Sunday, December 11, 2005

Good food and good cookbooks

I like good food. It's a taste that I think has changed over the years and it's only grown with both my advances in the culinary arts and teh fact that I married a woman who's a wonderful cook. Tonight we had a Green Chile Sausage Souffle with Mexican Rice and Broccoli and Carrots with Lime Dressing.

The rice was ok. For some reason it didn't cook all the way even though it was cooking for twice as long as the direction said and we added more water as it cooked to keep the moisture up. Amber thinks that maybe it was too hot at the beginning and water boiled off, affecting the rice's ability to absorb. Who knows, next time I think we'll do the prep work in the sauce pan on the stove (sauteing the onion, garlic and rice in oil) then combine the rest of the ingredients and water in the rice cooker.

The broccoli and carrots were pretty good. It was an interesting dressing that included dark sesame oil, soy sauce, honey and lime juice. The veggies were boiled for 5 min and then cooled quickly in a colander before serving. The taste wasn't perfect for the rest of the meal but it went pretty well.

The souffle was awesome. The recipe caught my eye because I made Southwestern Green Chile Poultry sausage last weekend with a friend and that was a key ingredient. Preparation involved roasting 8 peppers and pealing the skin off, slitting them and stuffing with a slice of monterey jack cheese. I'd never roasted peppers myself before and I'd always been a little afraid of the process. Last weekend however, we had to roast peppers for the sausage and my friend Matthew did it under the broiler and it went flawlessly so I lost my fear of the process. On top of the peppers was scattered the cooked sausage and we spooned the egg, milk, flour, spice mix over the top of that and baked it. Man, it was good. Definitely a recipe that we'll have to make again.

Right now, I've got some cracked wheat in our smaller crockpot cooking for breakfast in the morning and some pinto beans soaking in the big crockpot. I'll cook the pintos tomorrow and maybe make refried beans on Tuesday. We finally got the slow cooker cookbook that we ordered almost a month ago and both Amber and I are very glad of that. If anyone's in the market for a slow cooker cookbook, I definitely recommend Not Your Mother's Slow Cooker Cookbook. We "test drove" it from the library for a few weeks and it's definitely a keeper.

If you're a sausage lover, I highly recommend Bruce Aidells' Complete Sausage Book. The first half of the book is how to make sausage and a bunch of different types to make. The second half of the book is all sorts of recipes to cook with sausage. You don't have to make your own sausage to enjoy the book, but I'm pretty sure it helps. Last weekend I made French Toast Stuffed with Chicken and Apple Sausage (with Sauteed Apple Slices on top) and it was absolutely fantastic.

Saturday, December 10, 2005

Mmmm...Beer....

We just finished bottling our latest batch of beer. It's an Irish Stout that my dad and I started on Wednesday before Thanksgiving. I drank most of the bottle that didn't get entirely full and it's a pretty good one. Once this is ready to drink, in about two weeks, we'll have three different kinds of homebrew in the house. Right now, there's over three cases of beer that we've made.

It's odd, I used to be able to drink whatever kind of beer and didn't really have a taste for what's good or not. I mostly stayed with your lighter beers, lagers and such. Since I started brewing my own though, my requirements for what makes a good beer have definitely gone up and my tastes have strayed toward the darker flavors. We currently have 5 bottles of Porter, a little over a case of Brown Ale and now almost two full cases of Irish Stout. Oh, and there's 5 gallons of wine that I'll be bottling around the first of the year too.

I think that I'm set until after Christmas. I'll probably be pretty busy soon anyway, given that our baby is due "any day now".

Parenting and the fine art of thinking around corners

Our daughter just came down to show us the wonderful picture that she'd made by poking holes in a piece of paper with a pen. Amber was very encouraging and told Emma that she'd made a wonderful picture. My response wasn't as encouraging...

You see, she'd been poking holes in paper with a pen yesterday, on the carpet. I was concerned that this would leave marks in the carpet so I asked her not to do it. When I saw it again this morning, I felt upset that she'd gone and done what I asked her not to do. I asked her how she'd made her holes and she confirmed that it was by poking with a pen through on the carpet. Then I asked her what I'd said yesterday, and she said that yes, I'd asked her not to do it. Of course, then I asked her why she'd done it.
"Because I wanted to."

Hmm...well, I can ask her not to do it again but since that was a less than stellar success last time, I don't have much confidence in it working any better this time. Ok, time to be a parent. The action itself wasn't bad, it was just the way that she was doing it. A much better solution would be to find a way to facilitate what she wants to do in such a way that it doesn't damage the carpet. Aha! An old towel laid down under the paper would provide just the same environment that she gets from the carpet and who cares if an old towel gets a few ink stains!

Now, she's happy and can make her holes in the paper and I don't need to worry about the carpet. This really makes me think that our job as parents involves so much more than you'd originally think. It's easy to fall into the trap of simply delivering edicts from on high and creating unmanagable situations. We have to take a moment and think about the entire situation, look at it from all angles, consider the children's point of view and what our real issue with a specific task is. Once we've done that it's generally possible to find a solution to make everyone happy.