tags:

views:

525

answers:

2

Again at the Rhino Mocks Noob Wall

mockUI.Expect( x => x.Update( new Frame[] {Frame.MakeIncompleteFrame(1, 5)} ) );

This is the exact argument that I need to match. Via trace statements, I have verified that is the actual output as well i.e. the code behaves as intended but the test disagrees. RhinoMocks responds with

TestBowlingScorer.TestGamePresenter.TestStart:
Rhino.Mocks.Exceptions.ExpectationViolationException : IScoreObserver.Update([Frame# 1, Score =  0 Rolls [  5,  PENDING,  ]]); Expected #1, Actual #0.

A Frame object contains few properties but doesn't override Equals() yet (overridden ToString() seen above). Update receives an array of Frames; How do I setup this expectation? I see an Is.Matching constraint.. not sure how to use it or rather am concerned with the verbose nature of it.

I have a helper NUnit style custom Assert

public static void AssertFramesAreEqual(Frame[] expectedFrames, Frame[] actualFrames)
{
  // loop over both collections
     // compare attributes
}
+1  A: 

Verified works.. don't know if this is THE RhinoMocks way

var expectedFrames = new Frame[] { Frame.MakeIncompleteFrame(1, 5) };
mockUI.Expect( x => x.Update(null) )
            .IgnoreArguments()
            .Constraints( Is.Matching<Frame[]>( frames => HelperPredicates.CheckFramesMatch(expectedFrames, frames) ) );

The helper predicate is just a function that returns a boolean value - True on an exact match else false.

 public static bool CheckFramesMatch(Frame[] expectedFrames, Frame[] actualFrames)
  {
     // return false if array lengths differ
     // loop over corresponding elements
          // return false if any attribute differs
     // return true
  }
Gishu
+2  A: 

@Gishu, Yeah, that's it. I just also learned about the Arg<> static class which should allow you to do something like this:

mockUI.Expect( x => x.Update(Arg<Frame[]>
           .Matches(fs=>HelperPredicates.CheckFrames ( expected, fs)) ));

There is also the Arg<>.List configuration starting point which I have not explored yet but might be even better for what you want

George Mauer
Works if the param is a `List<T>` where T is a struct like `mockUI.Update(Arg<List<Frame>>.List.ContainsAll(list_of_expected_frames));` Didn't work for my `Frame[]` case, where Frame is a class which doesn't override Equals() - didn't spend too much time on it.
Gishu