c# 3.0 - MSTest with Moq - DAL setup -
I am new to Moq, and started on a project which is already in development. I am responsible for setting up unit tests for DatabaseFactory, a custom class that uses EnterpriseLibrary and looks like this:
public database CreateCommonDatabase () {CreateDatabaseInstance return (string.Empty ); } Private Static Database CreateDatabaseInstance (string foo) {var database = clientCode == string.Empty? DatabaseFactory.CreateDatabase ("COMMON"): New OracleDatabase (New Client Connection). GetConnectionString (foo)); Return database; }
Now, this is where it is used (ResultData is a type of dataset):
Public ResultData GetNotifications (string foo, String foo2, database database) {var errMsg = string.Empty; Var Retal = 0; Var ds = new dataset (); Var sqlClause = @ "[Some SELECT statements are here which uses FU]"; DB Commm CM = Database. Gate SQL String Commands (SQL Clause); Cm.CommandType = CommandType.Text; // Add parameter if (userSeq! = String.Empty) {database.AddInParameter (cm, ": foo2", dbippe. string, foo2); } Try {Ds = database.ExecuteDataSet (cm); } Hold (exception before) {retval = -99; ErrMsg = ex.Message; } Return new Respected Data (DS, Retell, Ermmog); }
Now, basically, the database was not passed as a parameter, but the method was making a new instance of DatabaseFactory using the CreateCommonDatabase method, and from there It was using. However, this class leaves ineffective because I can not really keep the database from killing, so I went with dependency injection and passed the database.
Now, I'm stuck, because there is no way to duplicate the database to test GetNotifications. I am thinking that I am reversing things, or if I am missing something, am I doing it right or should I reconsider how I have been established?
Edit to add more information *****
Want to test T database? I want to return an example of the DataAutation Class (above) ResultData, but all this I really want to test. If I take a level up then at the professional level, I have:
Public Dataset Gotonotification (string foo, string foo1, int return value, out string error message, database Database) {ResultData rd = new data Notification (). GetNotifications (foo, foo1, databases); Return value = rd.ResultValue; ErrorMessage = rd.ErrorMessage; Return Rd. Retrieve data; }
So, basically, the database was not passed, this was the data. The notification class that made it - but then, if I left it like this, then I could not hit the help of the database to check this business layer object I modified all the code to pass the database (Which is the base page of the web), but now I do not tell what to do next. I thought I was away from having a test of one unit from this solution, but apparently, I am wrong or I have a mental barrier on the right path.
If you have virtual methods, you should be able to create fake database objects. If they are not, then you have a problem.
I do not know how "database" is, but you have some options.
-
If you are the owner of the source code of the database, then I recommend removing an interface database instead of dealing with a database class type. This will eliminate some complexity and will give you extremely testable.
-
If you do not have access to the database class, you can always resolve it with another layer of an abstract. In this case many people use a repository pattern that wraps data access layer. Generally speaking in this matter, most people leave test classes for integration tests (without any separation), instead of unit tests.
Here you are using your test option # 1:
[TestMethod] Public Zero GetNotifications_PassedNullFoo_ReturnsData () {/ / Arrange fake & lt; IDbs & gt; Moccoby = new fake & lt; IDbs & gt; (); MockDB.Setup (db = & gt; db.ExecuteDataSet ()). Returns (new dataset () ...); // Act FooClass target = new fooClass (); Var result = target GetNotifications (empty, "Foo2", mockDB.Object); // Assign fears. Is true (result. Dataset rows number; gt; 0); }
My dataset is a little small, but hopefully this gives you a general idea.
Comments
Post a Comment