Mocking Database with using OpenAccess

3 posts, 0 answers
  1. Greg
    Greg avatar
    16 posts
    Member since:
    Sep 2013

    Posted 15 Oct 2013 Link to this post

    Please refer to the next posting, as I have removed this one.
  2. Greg
    Greg avatar
    16 posts
    Member since:
    Sep 2013

    Posted 15 Oct 2013 Link to this post

    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);
    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;
                return fh.FileID;
  3. DevCraft R3 2016 release webinar banner
  4. Stefan
    Stefan avatar
    198 posts

    Posted 16 Oct 2013 Link to this post

    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.

    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.
Back to Top