My Lessons Learned From Rhino.Mocks

January 25th, 2010

Recently I have begun working with Rhino.Mocks and I stumbled a little bit getting up to speed. My experience with mocks is from using JMock1 with a Java project. This is the first time I have used mocks on a .NET project so it took me a little bit getting used to the syntax.

From looking at all the tutorials and examples on the web about Rhino.Mocks I couldn’t really find anything that was a kick in the pants from my stand point. Most of the tutorials didn’t discuss the methods Rhino.Mocks framework in any detail, and would instead go into, somewhat, depth about the theory of mocking. I already know (or at least think I do) the theory of mocking so I was really wanting something to get me going quickly.

I found a video by the creator of Rhino.Mocks, Ayende, which really helped out. I wanted to put up here some of the key points. You can watch the video at http://www.ayende.com/hibernating-rhinos.aspx.

The ‘_mocks’ variable listed below is a reference to the mocks repository, this will basically contain all of your mocks:

var _mocks = new MockRepository();

The ReplayAll method, _mocks.ReplayAll(), puts the framework into record mode. You will need to call this method after creating your mocks and setting expectations. The Rhino.Mock framework will begin to track all the method calls and verify the expected counts (if any) are meet. In the video Ayende talks about this around 11:08, 28:10 & 29:50. There is not any issue with calling this method repeatedly, so having it in your TearDown method as a precaution will not cause any problems.

One other thing that sent me on a wild goose chase is the error message:

require a return value or an exception to throw

This will occur when you don’t move your tests from Record mode to Replay mode (typically you are missing a _mocks.ReplayAll() method call). What happens is you call a method such as ‘user.GetPurchases()’, and in my case I was making an assertion on the expected values. But the framework is waiting on you to specify an expectation or a return value, but since this is missing Rhino.Mocks can not handle it and so throws that exception.

As the name implies ‘_mocks.VerifyAll()’ will verify any outstanding expectations which were set by you, I have this in my TearDown method as well.

What’s the difference between Expect.Call and LastCall.Constraints?
As he discusses in the video at 36:55, this is due to a constraint the compiler enforces. If you had the following code:

class User {
  public void Save() { .... }
}

Then of course the compiler will not allow you to have syntax such as:

Expect.Call(user.Save())

You will need a way to execute the call, then set expectations on that … well, last call. Simple name huh? Shockingly he discusses this as well in the video at 36:55. Both of syntaxes are basically the same, the Expect.Call syntax is actually changed into the LastCall format. He shows how to mock out a void return type around 19:40.

Another exception/error you might stumble across is:

This action is invalid when the mock object is in verified state.

You will run across this while trying to setup a mock or expectation and in the record phase. My mistake was putting the creation of the MockRepository instance (the _mocks variable) in the TestFixtureSetup method – being this is only called once per the entire test class, my MockRepository was not being recreated. So I received the error – switching this to be what I expected, the Setup attribute, then all was well.

Something I have yet to look into is the difference between the following lines:

Expect.Call(order.PaidInFull).Return(false);

-- and --

SetupResult.For(order.PaidInFull).Return(false);

If you know of what the difference might be, I would be happy to hear.

Author: gorshing Categories: Programming Tags:

Using the blank constraint in Grails

November 29th, 2009

I have just begun using Grails for Less Out and have been accustomed to using the ‘def’ keyword when declaring any variables. I really don’t have any particular reason in doing it this way, I just wanted to use the features of Groovy when ever possible … when in Rome.

This past weekend I was working on an Action which is passed a Command, the Command has a few constraints and one of them was:

static constraints = { 
  name blank:false, nullable:false 
}

This is pretty harmless, but I was not able to get a test to pass. I was never able to get the blank constraint to fail.

I then started to hunt through the Grails source code and found the BlankConstraint source. More specifically, I think the culprit (somebody please prove me wrong here) is the ‘supports’ method.

public boolean supports(Class type) {
        return type != null && String.class.isAssignableFrom(type);
 
}

One thing I left out is I declared the ‘name’ variable as:

def name

The ‘supports’ method checks if String is assignable from the passed in type, and since you can think of def as an Object (documentation) then the method will return false.

Once I changed my declaration to:

String name

Then all my test validations worked correctly.

I could see how Grails would not want to throw an exception in this scenario (especially given this late in the game) but I log statement stating it is skipping over this contraints would be very much appreciated.

Author: gorshing Categories: General Tags:

Subversion, TortoiseSVN, and Putty

October 29th, 2009

I recently changed development machines and needed to setup TortoiseSVN again using SSH, and I ran into a problem where I could only access Subversion if it was the only authorized key. For my host I’m using Site5, but I’m sure this applies more generally than just for Site5. I already have another set of keys working for ssh, and that public key is the first line of my authorized_keys2 file (the ‘2′ is somewhat optional).

My url for svn is something similar to ’svn+ssh://domainname_svn/trunk’, I verified this just to make sure from a backup I have on another machine. So I knew I was using the right url, but I still wasn’t able to connect. I have pageant running and have both (one for ssh and one specifically for svn) keys loaded.

When I tried to check out a working copy with Tortoise, it would give me a error. When I would change my authorized_keys file on the server to only have the svn key then it worked just fine. Once again I knew the server was setup correctly as I had no problems previously.

What my problem was I had not specified a key under putty. I use putty’s saved sessions to specify a couple of options only for my svn connection. I had not specified anything under Connection -> SSH -> Auth -> Private Key.

Once I filled that in with the correct key then it didn’t matter what keys Pageant had loaded.

Author: gorshing Categories: Programming Tags:

Gossip According to Dave Ramsey

September 7th, 2009

I regularly listen to ‘The Dave Ramsey’ on my iPod and thought his rant on gossip in the work place was a pretty good piece.

How do you incorporate a no-gossip policy at work? (mp3 download 5.54MB 6 minutes)

Author: gorshing Categories: General Tags:

My Version of Qdoba’s Mexican Gumbo

August 25th, 2009

Although I feel ‘gumbo’ is a misnomer in this recipe, this is what my wife and I have been cooking for almost a year now. This is very similar to Qdoba’s (Qdoba Mexican Grill) recipe.

Ingredients:
2 cups cooked rice
1 can black beans, drained and rinsed
2-4 chicken breasts, grilled and sliced
1 pkg. Williams Country Store Tortilla Soup
1 can diced tomatoes (Rotel)
pico de gallo
sour cream
tortilla strips or tortilla chips (optional)

Notes:
Some people may like only two cups of rice but I use 3 cups, use your discretion on whether to use minute rice or real rice. I use Riceland in my rice cooker (http://www.amazon.com/gp/product/B0009E3F68/ref=ox_ya_oh_product). We also use an additional can of Bush’s black beans. For marinading the chicken we use whatever we have, sometimes we even grill it without any marinade/rub. I like the Williams Country Store Tortilla soup better than the Bear Creek, but you might try both of them and decide for yourself.

Get the chicken on the grill and soup going at the same time (saves time). When cooking the tortilla soup I add more water than it calls for, but other than that just follow the directions on the bag. Once the soup is done cooking, I dump in the beans and the chicken breasts (cut up in chunks). I keep the rice separate from the soup so people can serve their own desired ratios.

I like eating this all by itself and don’t use the pico, sour cream, and strips. But most people add up on this stuff, needless to say guacamole is fine too.

Author: gorshing Categories: General Tags:

Motherly Embrace of Process

May 6th, 2009

Today I was reading an article by Dave Nicolette and the following caught my eye: “Many people really, really want to curl up in the motherly embrace of Process”. I feel that typically most people do want this and therein lies the problem.

I wouldn’t want to believe this either, but I have been reading Influence by Robert B. Cialdini and in Chapter 6 he talks about “Authority – Directed Deference”. In this Dr. Cialdini references multiple studies done where people basically blindly follow what an established authority figure does or says. These are as simple as more people follow, what seems to be, an established business man across a street and progress up to nurses doing exactly what they think the doctor said (like putting ear drops into a patients R ear; the nurse interrupts this as rear and actually performs the administration of the medicine).

The team I have been working with since the beginning of the year has recently undergone a dramatic change in the developers on the team. There is currently only two other Valtech consultants along with myself, the others are employees of our client. On occasion management has told us exactly what we must do. One specific instance was a ‘mandate’ that we do not check in code against a single project for four days. I hope you understand how absurd this is. There were a few people who complied immediately, it seems like they felt it was the only way to deal with the situation. A few of my coworkers gently pointed out how bad of an idea it was, and a solution was agreed upon which helped out multiple teams.

It seems to me that most people will agree with ‘authority’ and it doesn’t really matter if this authority figure is real or make believe. People will continue to believe in the ‘process’ as this is the authority on the subject matter and must not be questioned (in their frame of mind). We who still believe in Agile Development will still have an up hill battle on our hands to coax people into believing there is a separate way to development software.

Author: gorshing Categories: General Tags:

Making Decisions in Small Iterations

March 7th, 2009

Earlier this year the New York Jets announced they had hired Rex Ryan as the new head coach. I have personally meet Rex on a couple of occasions so I have followed along and read many articles and comments about Rex’s decision. One gentleman in particular criticized for leaving the Ravens so quickly after the season was over; he felt as though the decision was made too hastily.

I have read about the different ways people react to circumstances, one way in classifying people is by using the DISC assessment. There are plenty of people which reside in the conscientious, meaning they will take in all sides to a story and put careful thought into what their decision is going to be. As you can obviously tell, this doesn’t play out to well for somebody in leading a professional football team. They will need to make quick decisions, otherwise, the game will be over … and probably their career. Rex knows the business of football tremendously better than any of us (I’m sure some people out there will debate that thought), and he knows exactly what he wants. He doesn’t need to take time to make a decision, he already knows what he is going after. It isn’t all that difficult to know what to do when you know what you are going after.

With Agile development this seems to carry over, the team has a common goal and we all are quick to make a decision, if we do not like what we have decided upon, then we will make another. By having small (one week) iterations this is possible and it allows us to identify the pain and adjust for it accordingly.

When I first heard about having one week iterations, I thought somebody had lost their mind. I didn’t feel as though much of anything could be accomplished. On the contrary it is very apparent of the progress getting made. We are also quick to make a decision for the sole reason of not having to live with the side effects for very long if something is not working.

Retrospectives also play a key role in this, as other team members bring to light a pain point which I was oblivious to. The reasons for myself not noticing these things are many, one of the more common is simply because they are at a different place in the project than I am. They are ahead of the pack and before I continue on and begin to feel the same pain, they identify the problem and a decision is made of what to do. On occasion a previous decision made in an earlier iteration is noted as not working as expected, we are then able to adapt quickly and change to get our work environment productive.

Author: gorshing Categories: General Tags:

This I have done

February 9th, 2009

It seems to me this should apply to most people. I do not think I have lived by its standard, maybe it is now time for me to walk the walk.

I Do Not Choose to Be a Common Man

It is my right to be uncommon—if I can.

I seek opportunity—not security. I do not wish to be a kept citizen, humbled and dulled by having the state look after me.

I want to take the calculated risk; to dream and to build, to fail and to succeed.

I refuse to barter incentive for a dole. I prefer the challenges of life to the guaranteed existence; the thrill of fulfillment to the stale calm of utopia.

I will not trade freedom for beneficence nor my dignity for a handout. I will never cower before any master nor bend to any threat.

It is my heritage to stand erect, proud and unafraid; to think and act for myself, enjoy the benefit of my creations and to face the world boldly and say, “This I have done.”

By Dean Alfange

Author: gorshing Categories: General Tags:

Take Care of Business

January 13th, 2009

I have begun (about a year ago) to appreciate quotes and saw this one tonight:

You can’t wait for inspiration. You have to go after it with a club. – Jack London

So what is important to you? Go do that right now and stop reading this.

Author: gorshing Categories: General Tags:

Maven Default Profiles

January 2nd, 2009

I have been using Maven for a while now and as such I have ran into my fair share of using profiles with Maven. As there are many other articles/posts about Maven profiles I will not be providing an overview here. I would like to delve into a particular area of interest when dealing with profiles and the activation aspect.

One of the many ways a profile can be activated is by ‘activeByDefault’, I currently use this both on our CI box and Windows (typically developers) box. These basically provide system properties for a database connection information and paths to property files. There has not been a time where the activeByDefault profiles has caused us any problems at all. So I was a little apprehensive when I saw James Lorenzen’s blog entry on the problems with using these types of profiles.

James professes (by way of providing a link to documentation) when a profile is activated by any other way then the so-called default profile is turned off. So I created a simple example pom in an attempt to replicate this and in doing so I ran across the same problem as he did. For the following, I use this pom as an example.

gorshing@touch ~
$ mvn clean
[INFO] Scanning for projects...
[INFO] ----------------------------------------------------------------------
[INFO] Building Unnamed - com.example:ProfileProject:pom:0.1-SNAPSHOT
[INFO]    task-segment: [clean]
[INFO] ----------------------------------------------------------------------
[INFO] [clean:clean]
[INFO] [antrun:run {execution: Testing Active by Default Profiles}]
[INFO] Executing tasks
     [echo] default profile val 54
     [echo] yep 42
[INFO] Executed tasks
[INFO] ----------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ----------------------------------------------------------------------
[INFO] Total time: 5 seconds
[INFO] Finished at: Wed Dec 31 12:29:32 CST 2008
[INFO] Final Memory: 3M/7M
[INFO] ----------------------------------------------------------------------
 
gorshing@touch ~
$ mvn clean -Pqual.val
[INFO] Scanning for projects...
[INFO] ----------------------------------------------------------------------
[INFO] Building Core000Build
[INFO]    task-segment: [clean]
[INFO] ----------------------------------------------------------------------
[INFO] [clean:clean]
[INFO] [antrun:run {execution: Testing Active by Default Profiles}]
[INFO] Executing tasks
     [echo] default profile val ${default.profile.val}
     [echo] yep 69
[INFO] Executed tasks
[INFO] ----------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ----------------------------------------------------------------------
[INFO] Total time: 4 seconds
[INFO] Finished at: Wed Dec 31 12:03:01 CST 2008
[INFO] Final Memory: 3M/7M
[INFO] ----------------------------------------------------------------------

As you can easily tell, this is exactly as the documentation specifies. The key difference between my experience and his is solely based on where the profiles are located. Given the setup I am using at work, the ‘activeByDefault’ profile isn’t in the pom itself, but is in ${M2_HOME}/conf/settings.xml.

I have added the following to my ${M2_HOME}/conf/settings.xml configuration, and using this pom (same as above only without the activeByDefault profile entry).

    <profile>
      <id>default.val</id>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <properties>
        <default.profile.val>54</default.profile.val>
      </properties>
    </profile>

As you can tell in the following listing, the activeByDefault truly does what one might expect.

gorshing@touch ~
$ mvn clean
[INFO] Scanning for projects...
[INFO] ----------------------------------------------------------------------
[INFO] Building Unnamed - com.example:ProfileProject:pom:0.1-SNAPSHOT
[INFO]    task-segment: [clean]
[INFO] ----------------------------------------------------------------------
[INFO] [clean:clean]
[INFO] [antrun:run {execution: Testing Active by Default Profiles}]
[INFO] Executing tasks
     [echo] default profile val 54
     [echo] some val 42
[INFO] Executed tasks
[INFO] ----------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ----------------------------------------------------------------------
[INFO] Total time: 1 second
[INFO] Finished at: Fri Jan 02 18:42:53 CST 2009
[INFO] Final Memory: 3M/7M
[INFO] ----------------------------------------------------------------------
 
gorshing@touch ~
$ mvn clean -Pqual.val
[INFO] Scanning for projects...
[INFO] ----------------------------------------------------------------------
[INFO] Building Unnamed - com.example:ProfileProject:pom:0.1-SNAPSHOT
[INFO]    task-segment: [clean]
[INFO] ----------------------------------------------------------------------
[INFO] [clean:clean]
[INFO] [antrun:run {execution: Testing Active by Default Profiles}]
[INFO] Executing tasks
     [echo] default profile val 54
     [echo] some val 69
[INFO] Executed tasks
[INFO] ----------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ----------------------------------------------------------------------
[INFO] Total time: 1 second
[INFO] Finished at: Fri Jan 02 18:43:11 CST 2009
[INFO] Final Memory: 3M/6M
[INFO] ----------------------------------------------------------------------

I am curious as to if this is expected results, or if this is a bug which might be resolved …. if that happens then it will most defiantly break some of my builds.

Something to look into and chew on.

Update: I asked on #maven @ irc.codehaus.org and received the following from wsmoak (these are of course her opinion):

activeByDefault was originally intended to be active only if no other profiles were explicitly activated … a cheap way to activate exactly one of a set of profiles … however I think it’s been “improved” since then. not sure exactly when/how … I generally avoid using it and don’t recommend it

Author: gorshing Categories: Programming Tags: