public void SqlParams_Should_Match_For_GetDataTableList() { var mockClient = new Mock <IModularDbClient>(); var paramList = new MockDataParameterCollection(); var input = JsonConvert.DeserializeObject <LocalizedContentFieldDataTablesInput>(@"{draw: 1,columns: [ {data: 'Id'} ], order: [{column:0, dir: 'asc'}],start: 0,length: -1,search: {value: 'some text', regex: false} }"); input.FieldType = DataUtility.GenerateRandom <string>(); input.FieldKey = DataUtility.GenerateRandom <string>(); input.ParentKey = DataUtility.GenerateRandom <string>(); input.GroupKey = DataUtility.GenerateRandom <string>(); input.GroupFilter = DataUtility.GenerateRandom <string>(); input.Language = DataUtility.GenerateRandom <string>(); var output = new DataTablesOutput(input); mockClient.Setup(m => m.GetConnectionBuilder(It.IsAny <string>())).Returns(new DbConnectionStringBuilder()); mockClient.Setup(m => m.GetConnection(It.IsAny <string>())).Returns((string connString) => { var dbConnection = new Mock <IDbConnection>(); dbConnection.Setup(m => m.CreateCommand()).Returns(() => { var dbCommand = new Mock <IDbCommand>(); dbCommand.Setup(m => m.Parameters).Returns(paramList); dbCommand.Setup(m => m.ExecuteReader()).Returns(() => { return(new Mock <IDataReader>().Object); }); return(dbCommand.Object); }); return(dbConnection.Object); }); mockClient.Setup(m => m.GetCommand(It.IsAny <string>(), It.IsAny <IDbConnection>())).Returns((string cmd, IDbConnection dbConnection) => { return(dbConnection.CreateCommand()); }); TRepo sut = new TRepo(mockClient.Object, ""); var o = sut.GetDataTableList(input); var order = input.order?.FirstOrDefault(); var colName = input.columns?.ToList().ElementAt(order?.column ?? 0).data; mockClient.Verify(m => m.GetConnectionBuilder(It.IsAny <string>())); mockClient.Verify(m => m.GetConnection(It.IsAny <string>())); mockClient.Verify(m => m.GetCommand(It.IsAny <string>(), It.IsAny <IDbConnection>())); mockClient.Verify(m => m.AddParameterWithValue(It.Is <string>(s => s == "@sSearch"), It.Is <string>(s => s == "%" + input.search.value + "%"), It.IsAny <IDbCommand>())); mockClient.Verify(m => m.AddParameterWithValue(It.Is <string>(s => s == "@nRowStart"), It.Is <int>(n => n == input.start), It.IsAny <IDbCommand>())); mockClient.Verify(m => m.AddParameterWithValue(It.Is <string>(s => s == "@nPageSize"), It.Is <int>(n => n == input.length), It.IsAny <IDbCommand>())); mockClient.Verify(m => m.AddParameterWithValue(It.Is <string>(s => s == "sSortDir"), It.Is <string>(s => s == order.dir), It.IsAny <IDbCommand>())); mockClient.Verify(m => m.AddParameterWithValue(It.Is <string>(s => s == "sSortCol"), It.Is <string>(s => s == colName), It.IsAny <IDbCommand>())); mockClient.Verify(m => m.AddParameterWithValue(It.Is <string>(s => s == "sFieldType"), It.Is <string>(s => s == input.FieldType), It.IsAny <IDbCommand>())); mockClient.Verify(m => m.AddParameterWithValue(It.Is <string>(s => s == "sFieldKey"), It.Is <string>(s => s == input.FieldKey), It.IsAny <IDbCommand>())); mockClient.Verify(m => m.AddParameterWithValue(It.Is <string>(s => s == "sParentKey"), It.Is <string>(s => s == input.ParentKey), It.IsAny <IDbCommand>())); mockClient.Verify(m => m.AddParameterWithValue(It.Is <string>(s => s == "sGroupKey"), It.Is <string>(s => s == input.GroupKey), It.IsAny <IDbCommand>())); mockClient.Verify(m => m.AddParameterWithValue(It.Is <string>(s => s == "sGroupFilter"), It.Is <string>(s => s == input.GroupFilter), It.IsAny <IDbCommand>())); mockClient.Verify(m => m.AddParameterWithValue(It.Is <string>(s => s == "sLanguage"), It.Is <string>(s => s == input.Language), It.IsAny <IDbCommand>())); mockClient.VerifyNoOtherCalls(); }
/// <summary> /// The create mock db command. /// </summary> /// <returns> /// </returns> public static Mock<IDbCommand> CreateMockDbCommand() { var mockParams = new MockDataParameterCollection(); var mockCommand = new Mock<IDbCommand>(); mockCommand.SetupAllProperties(); mockCommand.SetupGet(x => x.Parameters).Returns(mockParams); mockCommand.Setup(c => c.CreateParameter()).Returns( () => new Mock<IDbDataParameter>().SetupAllProperties().Object); var mockConnection = new Mock<IDbConnection>(); mockConnection.Setup(c => c.CreateCommand()).Returns(mockCommand.Object); mockCommand.SetupGet(c => c.Connection).Returns(mockConnection.Object); return mockCommand; }