CN102693182A - Repository mode using test-driven development in MVC (model view controller) - Google Patents

Repository mode using test-driven development in MVC (model view controller) Download PDF

Info

Publication number
CN102693182A
CN102693182A CN2012101647151A CN201210164715A CN102693182A CN 102693182 A CN102693182 A CN 102693182A CN 2012101647151 A CN2012101647151 A CN 2012101647151A CN 201210164715 A CN201210164715 A CN 201210164715A CN 102693182 A CN102693182 A CN 102693182A
Authority
CN
China
Prior art keywords
test
mvc
thesaurus
constructed fuction
repository
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN2012101647151A
Other languages
Chinese (zh)
Inventor
董阳
易川
张吉英
李德逸
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
SUZHOU BOYUAN RONGTIAN INFORMATION TECHNOLOGY CO LTD
Original Assignee
SUZHOU BOYUAN RONGTIAN INFORMATION TECHNOLOGY CO LTD
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by SUZHOU BOYUAN RONGTIAN INFORMATION TECHNOLOGY CO LTD filed Critical SUZHOU BOYUAN RONGTIAN INFORMATION TECHNOLOGY CO LTD
Priority to CN2012101647151A priority Critical patent/CN102693182A/en
Publication of CN102693182A publication Critical patent/CN102693182A/en
Pending legal-status Critical Current

Links

Images

Abstract

The invention discloses a repository mode using test-driven development in an MVC (model view controller). If the repository mode with the MVC and the test-driven development is used, a tester needs to build an interface for a data access class. When a controller method is subjected to a unit test, a mock repository can be instantiated according to the interface to simulate the operations of adding, revising, deleting and inquiring the data in the practical operation process. When the repository mode carries out the unit test, a real database server is not required; for the unit test for editing and deleting data, a clean database environment does not need to be prepared when each round of unit test is performed; a substitution point is provided for the unit test; and service logic can be easily tested without the database or other external dependencies.

Description

A kind of in MVC the thesaurus pattern of use test drive development
 
Technical field
The present invention relates to the method for a kind of use test drive development (TDD), be specifically related to a kind of in MVC the thesaurus pattern of use test drive development.
Background technology
At present in the unit testing of MVC, when controller method is carried out unit testing, when relating to the interpolation of calling data access, modification, deletion, inquiry, often need real database server support, could normal operation unit testing code.Through analysis, find to exist the problem that inconvenience realizes to unit testing in the past.
Its particular problem comprises:
When 1, at first performance element is tested, need real database server support;
2, the unit testing of editor and deleted data will be changed the state of database, and this just needs each unit testing of taking turns all to have the clean data lab environment;
3, when controller method is carried out unit testing, if need call certain method in another type in the method to be tested, and needs obtain the rreturn value of this method, the ability correct execution.If invoked method is not exploitation completion for a certain reason, in this case, want the method is tested, can only wait for type of being called exploitation completion.
Summary of the invention
Invention provide a kind of in MVC the thesaurus pattern of use test drive development; Purpose is to solve the dependence of unit testing to True Data storehouse and other external modules; A kind of novel interim thesaurus pattern is provided, dirigibility, the testability of bigger optimization unit testing.
The apprizing system of appearance treatment plant reaches above-mentioned technique effect for realizing above-mentioned technical purpose, and the present invention realizes through following technical scheme:
A kind of in MVC the thesaurus pattern of use test drive development, it is characterized in that, may further comprise the steps:
Step 1) is added thesaurus, the interface of an accessing database of definition;
Step 2) creates test and drive, realize the mock thesaurus;
Step 3) is added the test class support, two constructed fuctions of definition in the target test class;
First constructed fuction is the printenv constructed fuction, and second constructed fuction adopted the parameter of IContactRepository type, and said second constructed fuction will import said mock thesaurus into by unit testing; Said first constructed fuction will be created the instance of EF_ContactRepository class, and during method of operating in calling controller, the MVC pipeline will call said first constructed fuction;
Step 4) is added test, in said test class, writes method of testing, calls said second constructed fuction.
Principle of the present invention is:
The present invention uses has the thesaurus pattern of MVC and TDD, needs the tester to create interface for the data access class; When controller method is carried out unit testing, can go to simulate the interpolation to data, modification, deletion, inquiry in the actual moving process according to interface instance mock thesaurus.
Adopt one of principle of the TDD of MVC to be, a particular requirement in the driving operations method is answered in each test, and test should not verified database or other assemblies (be placed on these assemblies in unit testing and the integration testing of data access as far as possible and test).
The invention has the beneficial effects as follows:
When 1, performance element is tested, do not need real database server;
2, the unit testing of editor and deleted data need not prepared the clean data lab environment when carrying out each wheel unit test;
3, for unit testing provides substitution point, test service logic and need not database and other outside rely on like a cork.
Description of drawings
Fig. 1 is a process flow diagram of the present invention.
Embodiment
Below with reference to accompanying drawing and combine embodiment, specify the present invention.
Referring to shown in Figure 1, a kind of in MVC the thesaurus pattern of use test drive development, may further comprise the steps:
Step 1) is added thesaurus:
Use has the thesaurus pattern of MVC and TDD (TDD), needs the tester to create interface for the data access class.When controller method is carried out unit testing, can go to simulate the interpolation to data, modification, deletion, inquiry in the actual moving process according to interface instance mock thesaurus.
The interface of an accessing database of definition:
1.public?interface?IContactRepository
2.{
3.?void?CreateNewContact(Contact?contactToCreate);
4. void?DeleteContact(int?id);
5. Contact?GetContactByID(int?id);
6. IEnumerable<Contact>?GetAllContacts();
7. int?SaveChanges();
8.}
Step 2) create test and drive, realize the mock thesaurus:
1.class?InMemoryContactRepository?:?IContactRepository
2.{
3. private?List<Contact>?_db?=?new?List<Contact>();
4. public?Exception?ExceptionToThrow?{?get;?set;?}
5. public?void?SaveChanges(Contact?contactToUpdate)?{}
6. public?void?Add(Contact?contactToAdd)?{}
7. public?Contact?GetContactByID(int?id)?{}
8. public?void?CreateNewContact(Contact?contactToCreate)?{}
public?int?SaveChanges()?{}
9. public?IEnumerable<Contact>?GetAllContacts()?{}
10. public?void?DeleteContact(int?id)?{}
11.}
Step 3) is added test support, two constructed fuctions of definition in the target test class:
1.public?HomeController()?:?this(new?EF_ContactRepository())?{?}
2.public?HomeController(IContactRepository?repository)
3.{
4. _repository?=?repository;
5.}
One is the printenv constructed fuction; Another constructed fuction has adopted the parameter of IContactRepository type, and this constructed fuction will import the mock thesaurus into by unit testing.The printenv constructed fuction will be created the instance of EF_ContactRepository class, and during method of operating in calling controller, the MVC pipeline will call this constructed fuction.
Step 4) is added test:
Adopt one of principle of the TDD of MVC to be, a particular requirement in the driving operations method is answered in each test, and test should not verified database or other assemblies (be placed on these assemblies in unit testing and the integration testing of data access as far as possible and test).
In test class, write method of testing, call the ginseng constructed fuction (having adopted the parameter of IContactRepository type) that has to be tested type.
1.private?static?HomeController?GetHomeController(IContactRepository?repository)?{…}
Write method of testing
1.[TestMethod]
2.public?void?CreateTest?()?{…}
In sum, adopting the TDD of MVC is that unit testing provides substitution point, and the tester is the test service logic like a cork, and need not database and other outside items that relies on.
The foregoing description just is to let the one of ordinary skilled in the art can understand content of the present invention and enforcement according to this in order technical conceive of the present invention and characteristics to be described, to be its objective is, can not limit protection scope of the present invention with this.The variation or the modification of every equivalence that the essence of content has been done according to the present invention all should be encompassed in protection scope of the present invention.

Claims (1)

1. the thesaurus pattern of a use test drive development in MVC is characterized in that, may further comprise the steps:
Step 1) is added thesaurus, the interface of an accessing database of definition;
Step 2) creates test and drive, realize the mock thesaurus;
Step 3) is added the test class support, two constructed fuctions of definition in the target test class;
First constructed fuction is the printenv constructed fuction, and second constructed fuction adopted the parameter of IContactRepository type, and said second constructed fuction will import said mock thesaurus into by unit testing; Said first constructed fuction will be created the instance of EF_ContactRepository class, and during method of operating in calling controller, the MVC pipeline will call said first constructed fuction;
Step 4) is added test, in said test class, writes method of testing, calls said second constructed fuction.
CN2012101647151A 2012-05-25 2012-05-25 Repository mode using test-driven development in MVC (model view controller) Pending CN102693182A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN2012101647151A CN102693182A (en) 2012-05-25 2012-05-25 Repository mode using test-driven development in MVC (model view controller)

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN2012101647151A CN102693182A (en) 2012-05-25 2012-05-25 Repository mode using test-driven development in MVC (model view controller)

Publications (1)

Publication Number Publication Date
CN102693182A true CN102693182A (en) 2012-09-26

Family

ID=46858655

Family Applications (1)

Application Number Title Priority Date Filing Date
CN2012101647151A Pending CN102693182A (en) 2012-05-25 2012-05-25 Repository mode using test-driven development in MVC (model view controller)

Country Status (1)

Country Link
CN (1) CN102693182A (en)

Cited By (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN104375933A (en) * 2014-09-18 2015-02-25 广州华多网络科技有限公司 Unit testing method and device
CN105630679A (en) * 2015-12-23 2016-06-01 北京奇虎科技有限公司 Code testing method and apparatus
WO2016201995A1 (en) * 2015-06-16 2016-12-22 中兴通讯股份有限公司 Method and apparatus for implementation of test object construction, and service configuration test apparatus
CN109656806A (en) * 2018-10-29 2019-04-19 口碑(上海)信息技术有限公司 A kind of the playback test method and device of interface data
CN113110982A (en) * 2021-03-29 2021-07-13 青岛海尔科技有限公司 Data access layer verification method and device, storage medium and electronic device

Citations (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US6804796B2 (en) * 2000-04-27 2004-10-12 Microsoft Corporation Method and test tool for verifying the functionality of a software based unit
CN101046763A (en) * 2006-03-29 2007-10-03 盛趣信息技术(上海)有限公司 Implementing method of automatic test system based on scenario
CN101841433A (en) * 2009-03-17 2010-09-22 清华大学 Test model and method

Patent Citations (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US6804796B2 (en) * 2000-04-27 2004-10-12 Microsoft Corporation Method and test tool for verifying the functionality of a software based unit
CN101046763A (en) * 2006-03-29 2007-10-03 盛趣信息技术(上海)有限公司 Implementing method of automatic test system based on scenario
CN101841433A (en) * 2009-03-17 2010-09-22 清华大学 Test model and method

Non-Patent Citations (1)

* Cited by examiner, † Cited by third party
Title
MICROSOFT: "Walkthrough:Using TDD with ASP.NET MVC", 《HTTP://MSDN.MICROSOFT.COM/EN-US/LIBRARY/FF847525.ASPX》, 1 July 2010 (2010-07-01) *

Cited By (6)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN104375933A (en) * 2014-09-18 2015-02-25 广州华多网络科技有限公司 Unit testing method and device
WO2016201995A1 (en) * 2015-06-16 2016-12-22 中兴通讯股份有限公司 Method and apparatus for implementation of test object construction, and service configuration test apparatus
CN105630679A (en) * 2015-12-23 2016-06-01 北京奇虎科技有限公司 Code testing method and apparatus
CN105630679B (en) * 2015-12-23 2019-04-23 北京奇虎科技有限公司 The method and device of code tester
CN109656806A (en) * 2018-10-29 2019-04-19 口碑(上海)信息技术有限公司 A kind of the playback test method and device of interface data
CN113110982A (en) * 2021-03-29 2021-07-13 青岛海尔科技有限公司 Data access layer verification method and device, storage medium and electronic device

Similar Documents

Publication Publication Date Title
CN103425574B (en) A kind of method and device of test application program
US6507842B1 (en) System and method for importing and exporting test executive values from or to a database
CN104636147B (en) A kind of implementation method and system of cross-platform mobile application
CN105912473B (en) Mobile APP automatically testing platform and test method based on BDD
CN102693182A (en) Repository mode using test-driven development in MVC (model view controller)
CN107273286A (en) For the scene automatic test platform and method of task application
CN109032825B (en) Fault injection method, device and equipment
JP2012523644A (en) Use of spreadsheet user interface with CPI simulation system flowsheet
US9824000B1 (en) Testing calling code dynamically with random error injection based on user-specified configuration
CN105447051A (en) Database operation method and device
CN101311897A (en) Method and device for snatching at application program graphical user interface resource
US10095678B2 (en) Database user interfaces with flowsheets of a simulation system
CN105653438A (en) Method and device for cross-process automated testing of Android device
CN104572394A (en) Process monitoring method and device
CN107608663A (en) A kind of development approach and device of Mobile solution plug-in unit and Mobile solution
CN103049374B (en) Automatic testing method and device
CN103984769A (en) Use-case data management and storage method
CN103942140A (en) Automatic testing program conversion method
CN109284222B (en) Software unit, project testing method, device and equipment in data processing system
CN104394518B (en) A kind of note transmission method and SMS platform
CN104536745A (en) Task modification method and device
CN111966760A (en) Hive data warehouse-based test data generation method and device
Zhang et al. Research and implementation of cross-platform development of mobile widget
CN104679648A (en) Cross-application automatic testing method
CN115061928A (en) Micro-service automatic testing method and device, electronic equipment and storage medium

Legal Events

Date Code Title Description
C06 Publication
PB01 Publication
C10 Entry into substantive examination
SE01 Entry into force of request for substantive examination
C02 Deemed withdrawal of patent application after publication (patent law 2001)
WD01 Invention patent application deemed withdrawn after publication

Application publication date: 20120926