单元测试之NUnit和RhinoMocks
作者:网络转载 发布时间:[ 2015/8/4 15:02:39 ] 推荐标签:软件测试技术 NUnit
断言方法没有被调用
[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,测试类,和输出结果的位置等相关参数
本文内容不用于商业目的,如涉及知识产权问题,请权利人联系SPASVO小编(021-61079698-8054),我们将立即处理,马上删除。
相关推荐
iOS单元测试mocha、chai、sinon和istanbul实现百分之百的单元测试覆盖率关于单元测试的总结及思考编写更好的Java单元测试的7个技巧Android单元测试框架Robolectric3.0介绍(一)使用Kiwi单元测试总结单元测试如此重要,为什么你不知道Python单元测试??使用装饰器实现测试跳过和预期故障对Controller的单元测试写好单元测试的10个技巧单元测试的重要性Angular单元测试系列??Component、Directive、Pipe 以及ServiceAndroid单元测试的整理提升单元测试体验的利器--Mockito使用总结iOS UnitTest单元测试Vue的单元测试探索(二)
更新发布
功能测试和接口测试的区别
2023/3/23 14:23:39如何写好测试用例文档
2023/3/22 16:17:39常用的选择回归测试的方式有哪些?
2022/6/14 16:14:27测试流程中需要重点把关几个过程?
2021/10/18 15:37:44性能测试的七种方法
2021/9/17 15:19:29全链路压测优化思路
2021/9/14 15:42:25性能测试流程浅谈
2021/5/28 17:25:47常见的APP性能测试指标
2021/5/8 17:01:11热门文章
常见的移动App Bug??崩溃的测试用例设计如何用Jmeter做压力测试QC使用说明APP压力测试入门教程移动app测试中的主要问题jenkins+testng+ant+webdriver持续集成测试使用JMeter进行HTTP负载测试Selenium 2.0 WebDriver 使用指南