Beispiel #1
0
        public void LocalDbForSingleTest()
        {
            // The database will be recreated here if it already exists
            using (TempLocalDb db = new TempLocalDb("Test"))
            {
                // Create the schema in the database (This tool will work best if you are using some kind of database migration tool like FluentMigrator or DbUp)
                SchemaHelper.MigrateToCurrentSchema(db);

                // Call some code that interacts with the database
                DatabaseService dbService = new DatabaseService(db.ConnectionString);
                dbService.Insert("First", "Last");

                // Verify that the code worked correctly
                using (var conn = db.Open())
                    using (var cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = "SELECT * FROM [Person] WHERE [FirstName] = @FirstName AND [LastName] = @LastName";
                        cmd.Parameters.AddWithValue("@FirstName", "First");
                        cmd.Parameters.AddWithValue("@LastName", "Last");

                        using (var reader = cmd.ExecuteReader())
                        {
                            // Just verify that the record exists in the table
                            Assert.IsTrue(reader.Read());
                        }
                    }

                // OR Call some other methods to verify the operation worked
                Assert.IsNotNull(dbService.FirstOrDefault("First", "Last"));
            }
        }
        public void Insert_Works()
        {
            string firstName = "First-" + Guid.NewGuid().ToString();
            string lastName  = "Last-" + Guid.NewGuid().ToString();

            DatabaseService db = new DatabaseService(database.ConnectionString);

            // With a shared database, you can use transaction scopes and not commit the changes so that unit tests don't interfere with each other.
            using (TransactionScope trans = new TransactionScope())
            {
                db.Insert(firstName, lastName);

                Assert.IsNotNull(db.FirstOrDefault(firstName, lastName));
            }

            Assert.IsNull(db.FirstOrDefault(firstName, lastName));
        }