This project is read-only.


These examples are based on this class:
public class MyClass
  public virtual void HelloWorld1() { }
  public virtual string HelloWorld2(string value) { }
Here is the test SetUp:
public class MyStubs : AttachFramework
  public delegate void HelloWorld1();
  public Results Attach(HelloWorld1 method, Return action) { return base.Attach(method, action); }

  public delegate string HelloWorld2(string value);
  public Results Attach(HelloWorld2 method, Return action) { return base.Attach(method, action); }

MyStubs stub = new MyStubs();
MyClass myObject = stub.CreateObject<MyClass>();

Stub a method -

Stub a method with no return value:
Results r = stub.Attach(myObject.HelloWorld1, Return.Nothing));
Stub a method with a string return value:
Results r = stub.Attach(myObject.HelloWorld2, Return.Value("hello world"));
Stub a method to throw an exception:
Results r = stub.Attach(myObject.HelloWorld2, Return.Exception(new Exception("error")));
Stub a method to return multiple values in sequence:
Results r = stub.Attach(myObject.HelloWorld2, Return.MultipleValues("first","second","third"));
Stub a method with a custom action by using a delegate:
Results r = stub.Attach(myObject.HelloWorld2, Return.DelegateResult(
  delegate(object[] parameters) { return parameters[0] + "1234"; }

Verify results from Results -

Verify method was called:
Verify method was called 3 times:
Assert.AreEqual(3, r.CallCount);
Verify parameter values:
Assert.AreEqual("param1", r.Parameters[0]);
Verify parameter values from multiple calls:
Assert.AreEqual("call1", r.History[0].Parameters[0]);
Assert.AreEqual("call2", r.History[1].Parameters[0]);
Assert.AreEqual("call3", r.History[2].Parameters[0]);

Example tests -

// Arrange
Results r = stub.Attach(myObject.HelloWorld1, Return.Nothing));

// Act

// Assert

// Arrange
Results r = stub.Attach(myObject.HelloWorld2, Return.Value("hello world"));

// Act
string result = myObject.HelloWorld2("hello");

// Assert
Assert.AreEqual("hello world", result);
Assert.AreEqual("hello", r.Parameters[0]);

Last edited Jul 24, 2008 at 2:10 AM by jwanagel, version 6


No comments yet.