コード例 #1
0
        public void Mock_ExecuteDataset_WithFunction_Test()
        {
            var conn = new MockDbConnection();

            MockTable table1 = MockTable.WithColumns("Col1", "Col2")
                               .AddRow(11, 12);

            MockTable table2 = MockTable.WithColumns("Col3", "Col4")
                               .AddRow("MyString", 3.4);

            conn.Mocks
            .When(null)
            .ReturnsDataset(cmd => new[] { table1, table2 });

            using (var cmd = new DatabaseCommand(conn))
            {
                cmd.CommandText.AppendLine("SELECT ...");
                var result = cmd.ExecuteDataSet(new { Col1 = 0, Col2 = 0 },
                                                new { Col3 = "", Col4 = 0.0 });

                Assert.AreEqual(1, result.Item1.Count());          // 1 row
                Assert.AreEqual(11, result.Item1.First().Col1);
                Assert.AreEqual(12, result.Item1.First().Col2);

                Assert.AreEqual(1, result.Item2.Count());          // 1 row
                Assert.AreEqual("MyString", result.Item2.First().Col3);
                Assert.AreEqual(3.4, result.Item2.First().Col4);
            }
        }
コード例 #2
0
        public void Mock_ReturnsSimple_MockRow_Test()
        {
            var conn = new MockDbConnection();

            MockTable table = MockTable.WithColumns("Col1", "Col2")
                              .AddRow(11, 12)
                              .AddRow(13, 14);

            conn.Mocks
            .WhenAny()
            .ReturnsRow(table);

            DbCommand    cmd    = conn.CreateCommand();
            DbDataReader result = cmd.ExecuteReader();

            result.Read();

            Assert.AreEqual(11, result.GetInt32(0));
            Assert.AreEqual(12, result.GetInt32(1));

            result.Read();

            Assert.AreEqual(13, result.GetInt32(0));
            Assert.AreEqual(14, result.GetInt32(1));
        }
コード例 #3
0
        public void Mock_ReturnsSimple_MockTableWithNull_Test()
        {
            var conn = new MockDbConnection();

            MockTable table = MockTable.WithColumns("Col1", "Col2")
                              .AddRow(null, 12)
                              .AddRow(13, 14);

            conn.Mocks
            .WhenAny()
            .ReturnsTable(table);

            DbCommand    cmd    = conn.CreateCommand();
            DbDataReader result = cmd.ExecuteReader();

            result.Read();

            Assert.AreEqual(null, result.GetValue(0));
            Assert.IsTrue(result.GetFieldType(0) == typeof(object));

            result.Read();

            Assert.AreEqual(13, result.GetValue(0));
            Assert.IsTrue(result.GetFieldType(0) == typeof(object));
        }
コード例 #4
0
        public void Mock_ReturnsMultiple_MockTable_Test()
        {
            var conn = new MockDbConnection();

            MockTable table1 = MockTable.WithColumns("Col1", "Col2")
                               .AddRow(11, 12);
            MockTable table2 = MockTable.WithColumns("Col3", "Col4")
                               .AddRow("aa", 3.4);

            conn.Mocks
            .WhenAny()
            .ReturnsDataset(table1, table2);

            DbCommand    cmd    = conn.CreateCommand();
            DbDataReader result = cmd.ExecuteReader();

            Assert.IsTrue(result.Read());

            Assert.AreEqual(11, result.GetInt32(0));
            Assert.AreEqual(12, result.GetInt32(1));

            Assert.IsTrue(result.NextResult());
            Assert.IsTrue(result.Read());

            Assert.AreEqual("aa", result.GetString(0));
            Assert.AreEqual(3.4, result.GetDouble(1));
        }
コード例 #5
0
 private MockTable CreateMockTable()
 {
     return(MockTable.WithColumns(nameof(Tests.Student.Id),
                                  nameof(Tests.Student.FirstName),
                                  nameof(Tests.Student.LastName),
                                  nameof(Tests.Student.Grade),
                                  nameof(Tests.Student.Timestamp),
                                  nameof(Tests.Student.CreationDate),
                                  nameof(Tests.Student.ModificationDate),
                                  nameof(Tests.Student.DeletionDate)));
 }
コード例 #6
0
        public void UnitTest1()
        {
            var conn = new MockDbConnection();

            // When a specific SQL command is detected,
            // Don't execute the query to your SQL Server,
            // But returns this MockTable.
            conn.Mocks
            .When(cmd => cmd.CommandText.Contains("COUNT_EMPLOYEES") &&
                  cmd.Parameters.Count() == 0)
            .ReturnsTable(MockTable.WithColumns("Count")
                          .AddRow(14));

            // Call your "classic" methods to tests
            int count = GetNumberOfEmployees(conn);

            Assert.AreEqual(14, count);
        }
コード例 #7
0
        public void Mock_ExecuteRow_WithTable_Test()
        {
            var conn = new MockDbConnection();

            conn.Mocks
            .WhenAny()
            .ReturnsTable(
                MockTable.WithColumns("Col1", "Col2")
                .AddRow(10, 11)
                .AddRow(12, 13));

            using (var cmd = new DatabaseCommand(conn))
            {
                cmd.CommandText.AppendLine("SELECT ...");
                var result = cmd.ExecuteRow(new
                {
                    Col1 = default(int?),
                    Col2 = default(int),
                });

                Assert.AreEqual(10, result.Col1);
                Assert.AreEqual(11, result.Col2);
            }
        }
コード例 #8
0
        public void Mock_Dataset_Func_Test()
        {
            var conn = new MockDbConnection();

            MockTable table1 = MockTable.WithColumns("Col1", "Col2")
                               .AddRow(11, 12);

            MockTable table2 = MockTable.WithColumns("Col3", "Col4")
                               .AddRow("MyString", 3.4);

            conn.Mocks
            .WhenAny()
            .ReturnsDataset(mockCmd =>
            {
                Assert.AreEqual("SELECT ...", mockCmd.CommandText);
                return(new MockTable[] { table1, table2 });
            });

            // First DataTable
            DbCommand cmd = conn.CreateCommand();

            cmd.CommandText = "SELECT ...";
            DbDataReader result = cmd.ExecuteReader();

            Assert.IsTrue(result.Read());

            Assert.AreEqual(11, result.GetInt32(0));
            Assert.AreEqual(12, result.GetInt32(1));

            // Second DataTable
            Assert.IsTrue(result.NextResult());
            Assert.IsTrue(result.Read());

            Assert.AreEqual("MyString", result.GetString(0));
            Assert.AreEqual(3.4, result.GetDouble(1));
        }