单元测试利器:Rhino Mocks

    Rhino Mocks是款单元测试工具,它功能非常强大。用它能轻松构建出测试需要的类或接口,而不需要编写繁琐的测试代码,咱们来看看它的效果吧!

    测试代码

 public abstract class People
    {
        public virtual string FirstName { get; set; }
        public virtual string LastName { get; set; }
        public virtual int Age { get; set; }
        public virtual bool Sex { get; set; }
        public IExtensionAction favoriteSport;

        public void IntroduceMyself()
        {
            
            Console.WriteLine(string.Format("Hello My Name is {0} {1}. I'm a {2} and {3} years old now. Nice to meet you! ", LastName, FirstName,this.Sex?"boy":"girl",Age.ToString()));
        }

        //which thing i can do
        public abstract void Do();
        public virtual void ExtensionDo()
        {
            if (favoriteSport != null)
            {
                Console.WriteLine(favoriteSport.Introduce());
            }
            else
            {
                Console.WriteLine("I just like sleeping in my free time.");
            }
        }
        public virtual string Bomb(string arg1, string arg2)
        {
            return "nothing!";
        }

        public abstract string AbstractMethod();
    }



 public class Student:People
    {
        public override void Do()
        {
            Console.WriteLine("I'm a student, i still study in school.");
        }

        public virtual string IntroduceMajor()
        {
            return "";
        }


        public override string AbstractMethod()
        {
            throw new NotImplementedException();
        }
    }



   public interface IExtensionAction
    {
      string Introduce();
      string SportType { get; set; }
    }


Mock代码1

            //模仿Student类中的虚拟方法IntroduceMajor
            Student jim = MockRepository.GenerateMock<Student>();
            jim.Stub(x => x.IntroduceMajor()).Return("My major is software engineering.");
            Console.WriteLine(jim.IntroduceMajor());

 


Mock代码2

            //模仿Student类中的抽象方法AbstractMethod
            Student jim = MockRepository.GenerateMock<Student>();
            jim.Stub(x => x.AbstractMethod()).Return("This is a abstract method");
            Console.WriteLine(jim.AbstractMethod());


 

Mock代码3

            //根据输入的参数值来决定模仿方法的返回值
            People clark = MockRepository.GenerateMock<People>();
            clark.Stub(x => x.Bomb(Arg<string>.Is.Anything, Arg<string>.Is.Anything)).Return("You can input any parameters");
            Console.WriteLine(clark.Bomb(null, null));

            People Bank = MockRepository.GenerateMock<People>();
            Bank.Stub(x => x.Bomb(Arg<string>.Matches(a1 => a1.StartsWith("B") && a1.EndsWith("K")), Arg<String>.Is.Equal("Super"))).Return("You must input special parameters");
            Console.WriteLine(Bank.Bomb("BanK", "Super"));

 

 

 

Mock代码4

            //替换调用模仿的方法
            People joe = MockRepository.GenerateMock<People>();
            joe.Stub(x => x.ExtensionDo()).Do(new Action(() => Console.WriteLine("I'm not ExtensionDo method!")));
            joe.ExtensionDo();

 


Mock代码5

            //重装模拟方法
            People king = MockRepository.GenerateMock<People>();
            king.Stub(x => x.Do()).Do(new Action(() => Console.WriteLine("Go!")));
            king.Stub(x => x.Do()).Do(new Action(() => Console.WriteLine("Reload!")));

            king.Do();
            king.Do();

            king.BackToRecord(BackToRecordOptions.All);//重装模拟方法
            king.Replay();

            king.Stub(x => x.Do()).Do(new Action(() => Console.WriteLine("Reload!")));
            king.Do();

 

 

 

Rhino Mocks下载

 

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值