断言方法没有被调用
  [Test]
  public void When_user_forgot_password_should_save_user()
  {
  var stubUserRepository = MockRepository.GenerateStub<IUserRepository>();
  var stubbedSmsSender = MockRepository.GenerateStub<ISmsSender>();
  User theUser = new User { HashedPassword = "this is not hashed password" };
  stubUserRepository.Stub(x => x.GetUserByName("ayende")).Return(theUser);
  stubUserRepository.Stub(x => x.Save(theUser));
  var controllerUnderTest = new LoginController(stubUserRepository, stubbedSmsSender);
  controllerUnderTest.ForgotMyPassword("ayende");
  stubUserRepository.AssertWasCalled(x => x.Save(theUser));
  stubbedSmsSender.AssertWasNotCalled(x => x.SendMessage(null));
  }
  对模拟对象设置期望
[Test]
public void Method_NoReturnValue()
{
MockRepository mocks = new MockRepository();
ICustomerService customer = mocks.StrictMock<ICustomerService>();
using (mocks.Record())
{
customer.BuyBook(null);
}
}
[Test]
public void Method_HasReturnValue()
{
MockRepository mocks = new MockRepository();
var func = mocks.StrictMock<Func<string,string>>();
using (mocks.Record())
{
Expect.Call(func("params")).Return("Result");
}
}
[Test]
public void Method_OutParameter()
{
MockRepository mocks = new MockRepository();
var func = mocks.DynamicMock<DoThing1>();
string strOut = "ABC";
using (mocks.Record())
{
Expect.Call(func("", out strOut)).Return("Result").OutRef("xxx");
}
mocks.ReplayAll();
Assert.AreEqual("Result", func("", out strOut));
Assert.AreEqual("xxx", strOut);
mocks.VerifyAll();
}
[Test]
public void Method_RefParameter()
{
MockRepository mocks = new MockRepository();
var func = mocks.DynamicMock<DoThing2>();
string strRef = "ABC";
using (mocks.Record())
{
Expect.Call(func("", ref strRef)).Return("Result").OutRef("xxx");
}
mocks.ReplayAll();
Assert.AreEqual("Result", func("", ref strRef));
Assert.AreEqual("xxx", strRef);
mocks.VerifyAll();
}
[Test]
public void Method_Options()
{
MockRepository mocks = new MockRepository();
var func = mocks.DynamicMock<DoThing>();
using (mocks.Record())
{
//返回值
Expect.Call(func("")).Return("");
//异常
Expect.Call(func("")).Return("").Throw(new Exception());
//方法允许使用的次数
Expect.Call(func("")).Return("").Repeat.Any();
Expect.Call(func("")).Return("").Repeat.Once();
Expect.Call(func("")).Return("").Repeat.Twice();
Expect.Call(func("")).Return("").Repeat.Times(3);
//忽略方法参数
Expect.Call(func("")).Return("").IgnoreArguments();
}
}
public delegate string DoThing(string s1);
public delegate string DoThing1(string s1, out string outs1);
public delegate string DoThing2(string s1, ref string ref1);
  设置期望时使用约束
[Test]
public void Constraints_Is_1()
{
MockRepository mocks = new MockRepository();
var customer = mocks.DynamicMock<ICustomerService>();
Expect.Call(customer.ShowTitle(""))
.Return("字符约束")
.Constraints(Rhino.Mocks.Constraints.Is.Matching<string>(x => x.StartsWith("cnblogs")));
mocks.ReplayAll();
Assert.AreEqual("字符约束", customer.ShowTitle("cnblogs my favoured"));
}
[Test]
public void Constraints_Property_OK()
{
MockRepository mocks = new MockRepository();
var customer = mocks.DynamicMock<ICustomerService>();
using (mocks.Record())
{
customer.BuyBook(null);
LastCall.On(customer).Constraints(Rhino.Mocks.Constraints.Property.Value("Name", "ABC"));
}
mocks.ReplayAll();
customer.BuyBook(new Book { Name = "ABC" });
mocks.VerifyAll();
}
[Test]
public void Constraints_Property_ShouldFail()
{
MockRepository mocks = new MockRepository();
var customer = mocks.DynamicMock<ICustomerService>();
using (mocks.Record())
{
Expect.Call(delegate { customer.BuyBook(null); }).Constraints(Rhino.Mocks.Constraints.Property.Value("Name", "ABC"));
}
mocks.ReplayAll();
customer.BuyBook(new Book { Name = "DDDABC" });
mocks.VerifyAll();
}
[Test]
public void Constraints_List_OK()
{
MockRepository mocks = new MockRepository();
var customer = mocks.DynamicMock<ICustomerService>();
Expect.Call(customer.SumPrice(null))
.Return(100)
.Constraints(Rhino.Mocks.Constraints.List.Equal(new int[]{100}));
mocks.ReplayAll();
Assert.AreEqual(100, customer.SumPrice(new int[]{100}));
}
[Test]
public void Constraints_List_ShouldFail()
{
MockRepository mocks = new MockRepository();
var customer = mocks.DynamicMock<ICustomerService>();
Expect.Call(customer.SumPrice(null))
.Return(100)
.Constraints(Rhino.Mocks.Constraints.List.Equal(new int[] { 10 }));
mocks.ReplayAll();
Assert.AreEqual(100, customer.SumPrice(new int[] { 100 }));
}
[Test]
public void Constraints_Text()
{
MockRepository mocks = new MockRepository();
var customer = mocks.DynamicMock<ICustomerService>();
Expect.Call(customer.ShowTitle(""))
.Return("字符约束")
.Constraints(Rhino.Mocks.Constraints.Text.StartsWith("cnblogs") && Rhino.Mocks.Constraints.Text.EndsWith("!"));
mocks.ReplayAll();
Assert.AreEqual("字符约束", customer.ShowTitle("cnblogs my favoured!"));
}
  设置期望时返回动态值(Do扩展方法的应用)
  [Test]
  public void SayHelloWorld()
  {
  MockRepository mocks = new MockRepository();
  INameSource nameSource = mocks.DynamicMock<INameSource>();
  Expect.Call(nameSource.CreateName(null, null))
  .IgnoreArguments()
  .Do(new NameSourceDelegate(Formal));
  mocks.ReplayAll();
  string expected = "Hi, my name is Ayende Rahien";
  string actual = new Speaker("Ayende", "Rahien", nameSource).Introduce();
  Assert.AreEqual(expected, actual);
  }
  delegate string NameSourceDelegate(string first, string surname);
  private string Formal(string first, string surname)
  {
  return first + " " + surname;
  }
  创建模拟对象对象注意事项:
  如果是类,必须包含无参数构造函数,被模拟的方法必须是虚方法;
  无法模拟静态类和静态方法;
  尽量通过被测试类的属性来设置被模拟对象(少用构造函数来建立依赖);
  如何运行测试用例
  方式1(从VS中启动nunit.exe,可以对测试代码和产品代码进行调试):
  在项目属性中的Debug面板中选中start external program并设置为{NUnitRoot}in unit.exe,然后启动测试工程。启动之后,打开测试工程对应的dll(一般是在*inDebug下)
  运行你希望执行的测试用例
  方式2
  打开{NUnitRoot}in unit.exe,并打开测试工程对应的dll来运行你希望执行的测试用例
  方式3(这个方式多用于持续集成环境中)
  通过nunit-console.exe来运行,你必须在参数中指定你要运行dll,测试类,和输出结果的位置等相关参数