?????????????б?????
????[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??????????????????λ?????????