This is a migrated thread and some comments may be shown as answers.

Mocking Database with using OpenAccess

2 Answers 123 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Greg
Top achievements
Rank 1
Greg asked on 15 Oct 2013, 05:00 PM
Please refer to the next posting, as I have removed this one.

2 Answers, 1 is accepted

Sort by
0
Greg
Top achievements
Rank 1
answered on 15 Oct 2013, 07:47 PM
I am having a hard time seeing how to test something like the following.  This method updates a record in the database.  So how should this be approached, as I would like to have it update, verify the record was updated, then rollback the transaction at the end of the test?

If I just mock the database, that is a valid unit test but I am also looking for an integration test that verifies that the update actually works and that the database is updated.  But I don't see how I can create a test that will roll this back, as SaveChanges() executes a commit.

So how would you suggest that I handle a unit test and an integration test for the CreateFileHistoryRecord() function?

          private readonly string _connectionString;
       private readonly ELAEntitiesModel _db;
       /// <summary>
       /// Constructor. Injects the connection string and creates the OpenAccess domain model object.
       /// </summary>
       /// <param name="connectionString"></param>
       public FileHistoryDao(string connectionString)
       {
           _connectionString = connectionString;
           _db = new ELAEntitiesModel(connectionString);
       }
          public
int CreateFileHistoryRecord(string filePath, string filename, Enumerations.ElaImportFileStatus status, Enumerations.ElaImportFileTypes fileType)
        {
            var fh = new ELA_FileHistory();
            fh.FileName = filename; 
            fh.FilePath = filePath;
            fh.ImportFileTypeID = (int)fileType;
            fh.Status = Convert.ToString(status);
            fh.RecordsProcessed = 0;
            fh.RecordsWithErrors = 0;
            fh.LastUpdated = DateTime.Now;
            _db.Add(fh);
            _db.SaveChanges();
  
            return fh.FileID;
        }
0
Stefan
Telerik team
answered on 16 Oct 2013, 06:14 AM
Hi Jon,

JustMock is not particularly useful for creating integration tests. After all, its main purpose is isolating dependencies.

In your case you'd like to do some changes to the database, assert that the changes are made (possibly using a second query that checks the DB's data) and then rollback. The pattern isn't much different than the regular Arrange/Act/Assert pattern. It's just that in your Arrange phase, you will have to bootstrap a fresh database (clone an existing one, maybe?). Then, in your Assert phase, you will query the DB and check that all necessary changes were indeed made. In the test cleanup just drop the database.

JustMock can't help you with writing integration tests. You will pretty much have to roll your own code and test structure that lets you quickly add new integration tests.

Regards,
Stefan
Telerik
Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
Tags
General Discussions
Asked by
Greg
Top achievements
Rank 1
Answers by
Greg
Top achievements
Rank 1
Stefan
Telerik team
Share this question
or