Hendrik Swanepoel

an Anonymous delegate based nmock action

I always find it tiring writing IAction implementations for nmock2 tests, so I wrote the following thingy, which basically allows you to specify anonymous delegates for an action.

public delegate void ActionPerformer(Invocation invocation);

public class DelegateAction : IAction
{
  private readonly ActionPerformer _actionPerformer;
  private readonly string _actionDescription;
  public DelegateAction(ActionPerformer actionPerformer, string actionDescription)
  {
    _actionPerformer = actionPerformer;
    _actionDescription = actionDescription;
  }

  public void Invoke(Invocation invocation)
  {
    _actionPerformer(invocation);
  }
  public void DescribeTo(TextWriter writer)
  {
    writer.Write(_actionDescription);
  }
}

You use it as such:

 

Expect.Once.On(_scheduler).Method("GetNext").WithNoArguments().Will(new DelegateAction(delegate(Invocation   invocation)
{
//whatever you want to do - even asserts
invocation.Result = whateveryou want to return, even variables before this statement;
},
"Whatvever you are performing with the action"));

Comments

kzuomgyqdus said:

O9zOfs  <a href="echmnsdrrzsq.com/.../a>, [url=http://egvssdjhanmd.com/]egvssdjhanmd[/url], [link=http://bthdphwpzfch.com/]bthdphwpzfch[/link], http://twjqaghthrrn.com/

# July 23, 2008 3:00 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: