Mock Test framework

Source: Internet
Author: User

First, preface

Using mock frames for unit testing, you can simulate data using interface methods already developed by the current system. (not finished, slowly perfected)

Ii. examples

1. Quoting MOQ

2. Define an interface and method as a test

    Public InterfaceIAnimal {//create animals, return to create success        BOOLCreateanimal (stringname); //There are output parameters in the method parameter        BOOLAnimaldance (stringName out stringmsg); stringEat (stringFood ); intGetCount (); intFindeven (inteven); intFindrange (intnum); stringFindregex (stringstr); stringName {Get;Set; } }

3.Test

 Static voidMain (string[] args) {Console.WriteLine ("------------------------------------------------------------------------"); //1. Create a mock based on the interface            varMock =NewMock<ianimal>(); //2. Configure the method to be simulated, call the method passed in parameters, return True when the panda is passed, other parameters return FaleMock. Setup (A = A.createanimal ("Panda")). Returns (true); ; //3. Call the mock to configure a good methodConsole.WriteLine (mock. Object.createanimal ("Panda"));//trueConsole.WriteLine (mock. Object.createanimal ("Panda"));//falseConsole.WriteLine ("------------------------------------------------------------------------"); stringmsg ="Dancing"; //configures the method to be emulated, invokes the method passed in the parameter (also passed in the output parameter), returns True when the peacock is passed, other parameters return FaleMock. Setup (A = A.animaldance ("Peacock", outmsg)). Returns (true); stringOutmsg =""; Console.WriteLine (mock. Object.animaldance ("Peacock", outOUTMSG));//true, the output parameter is danceConsole.WriteLine (outmsg); Console.WriteLine (mock. Object.animaldance ("Big Peacock", outOUTMSG));//false, the output parameter is dancingConsole.WriteLine (outmsg); Console.WriteLine ("------------------------------------------------------------------------"); //When you configure the call method, the argument is cabbage and throws an exception.Mock. Setup (A = A.eat ("Cabbage")). Throws (NewArgumentException ("wrong.")); Console.WriteLine (mock. Object.eat ("Vegetables")); //Console.WriteLine (mock. Object.eat ("cabbage")); //Throw ExceptionConsole.WriteLine ("------------------------------------------------------------------------"); //return different values at each call, callback ()            intCount =0; Mock. Setup (A= A.getcount ()). Returns (() = count). Callback (() = count++); Console.WriteLine (mock. Object.getcount ());//0Console.WriteLine (mock. Object.getcount ());//1Console.WriteLine (mock. Object.getcount ());//2Console.WriteLine (mock. Object.getcount ());//3Console.WriteLine ("------------------------------------------------------------------------"); //no matter what string you enter, the output food is two characters it.isany<string> ()Mock. Setup (A = A.eat (it.isany<string> ())). Returns ((strings) = ="Food"); Console.WriteLine (mock. Object.eat ("Bread")); Console.WriteLine ("------------------------------------------------------------------------"); //The number entered is even, the number itself is returned, otherwise the default value is 0 it.is<int>Mock. Setup (A = A.findeven (it.is<int> (i = i%2==0))). Returns ((intb) = =b);  for(inti =0; I <5; i++) {Console.WriteLine (mock.            Object.findeven (i)); } Console.WriteLine ("------------------------------------------------------------------------"); //value of Match range it.isany<string> ()Mock. Setup (A = A.findrange (it.isinrange<int> (5,8, range.inclusive)). Returns ((intb) = =b);  for(inti =2; I < One; i++) {Console.WriteLine (mock.            Object.findrange (i)); } Console.WriteLine ("------------------------------------------------------------------------"); //match Regular Expression it.isany<string> ()Mock. Setup (A = A.findregex (It.isregex ("[4-7]"))). Returns ((stringb) = =b); Console.WriteLine (mock. Object.findregex ("2")); Console.WriteLine (mock. Object.findregex ("5"));//only 5 compliant, output 5 onlyConsole.WriteLine (mock. Object.findregex ("8")); Console.WriteLine ("------------------------------------------------------------------------"); //PropertiesMock. Setup (s = = s.name). Returns ("Properties Oh"); Console.WriteLine (mock.            Object.name);        Console.readkey (); }

Mock Test framework

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.