51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 4799|回复: 0
打印 上一主题 下一主题

[原创] NMock2入门经验

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2008-4-24 13:35:45 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
每个做单元测试的人都应该一种技术--MOCK~什么是MOCK?其实没什么神秘的,就是打桩~网上流行的一些.NET的mock框架有:NMock2,Rhino.Mocks等……
NMock2是ThoughtWorks维护的一个开源项目,不知道是不是ThoughtWorks的风格就是敏捷开发基本没有文档,网络上很难找到关于NMock2的文档。而且网上给的例子也很简单,简单到需要自己补充一些东西上去才能跑通。还是用官网上的那个例子:现在有一个银行系统,它实现了一个功能,就是允许两个同名帐户之间实现转账功能。但是很不巧的是,在两个帐户之间转账涉及到汇率的问题,而这个汇率又是天天变的,需要从一个外部的服务来实现的。简单来说就是:
一个接口:IAccountService,实现转账的功能
    public interface IAccountService
    {
         void TransferFunds(Account from, Account to, double amount);
    }
一个接口:ICurrencyService,实现获取汇率的功能
    public interface ICurrencyService
    {
        double GetConversionRate(string fromCurrency, string toCurrency);
    }
一个类:Account,就是一个一个的帐户
public class Account
    {
        private double _balance;
        private string _name;
        private string _currency;
        public double balance
        {
            get
            {
                return _balance;
            }
            private set
            {
                _balance = value;
            }
        }
        public string name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }
        public string Currency
        {
            get
            {
                return _currency;
            }
            set
            {
                _currency = value;
            }
        }
        public Account()
        {
            this.name = null;
            this.Currency = null;
        }
        public Account(string name, string currency)
        {
            this.name = name;
            this.Currency = currency;
        }
        public void Withdraw(double amount)
        {
            this.balance -= amount;
        }
        public void Deposit(double amount)
        {
            this.balance += amount;
        }
}
还有一个实现了IAccountService接口的AccountService类
public class AccountService : IAccountService
    {
        private readonly ICurrencyService currencyService;
        public AccountService(ICurrencyService currencyService)
        {
            this.currencyService = currencyService;
        }
        public void TransferFunds(Account from, Account to, double amount)
        {
            from.Withdraw(amount);
            double conversionRate = currencyService.GetConversionRate(from.Currency, to.Currency);
            double convertedAmount = amount * conversionRate;
            to.Deposit(convertedAmount);
        }
    }
这里我们就对ICurrencyService接口实现一个Mock,给测试程序传递一个假的对象
首先在NUnit的代码里面定义需要用到的对象
        Mockery mocks;
        ICurrencyService mockCurrencyService;
        IAccountService accountService;
然后在单元测试的TestInitialize方法里面初始化mock
        mocks = new Mockery();  //初始化mocks
        mockCurrencyService = mocks.NewMock<ICurrencyService>(); //产生一个mock对象
        accountService = new AccountService(mockCurrencyService); //用mock对象初始化accountService
最后就是测试的代码:
        public void ShouldUseCurrencyServiceToDetermineConversionRateBetweenAccounts()
        {
            //生成2个帐号
            Account canadianAccount = new Account("John Doe", "CAD");
            Account britishAccount = new Account("Jane Doe", "GBP");
            /给英国人存100块
            britishAccount.Deposit(100);

            Expect.Once.On(mockCurrencyService).
                Method("GetConversionRate").
                With("GBP", "CAD").
                Will(Return.Value(2.20));//对于mockCurrencyService对象,如果他的GetConversionRate方法被调用,而且参数分别是("GBP", "CAD"),那么就返回一个值2.20
            
            accountService.TransferFunds(britishAccount, canadianAccount, 100);
            Assert.AreEqual((double)0, britishAccount.balance,0.00001);
            Assert.AreEqual((double)220, canadianAccount.balance,0.00001);
            mocks.VerifyAllExpectationsHaveBeenMet();
        }
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-5-8 19:05 , Processed in 0.069287 second(s), 27 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表