I have a business object that I am mocking (using Moq) for business tests. When I call the object, it calls another business object which goes to the data layer. I can set a return to calls within the object that I wrote, but how can I prevent it from calling the other object and going into the data layer?
Is there a standard practice for preventing this? Apologies if this isn't clear enough; I will answer any questions as they are asked. Thanks!
Here is an example of the code, simplified just a bit:
Business Test
Dim mockPer As New Mock(Of IYTDMarketShrink)
Dim cLogging As New CLogProduction
Dim cConfig As New cConfiguration
cConfig.Environment = Ent.MeijerEnvironmentEnum.DEVELOPMENT
'Create items for use in test
Dim results As DataTable
Dim dsTestData As New System.Data.DataSet
dsTestData.ReadXmlSchema("MarketShrinkPerTestData.xsd")
dsTestData.ReadXml("MarketShrinkPerTestData.xml") 'Return values for the IYTDMarketShrink data layer
Dim target As New CMarketShrinkPer(CType(mockPer.Object, IYTDMarketShrink), cLogging, cConfig)
'Assign parameter values for the mock object
target.FiscalYear = 2009
target.FiscalPeriod = 9
target.ReportType = "CPPTS"
'Set the return value of the mocked function, call the function
mockPer.Setup(Function(x As IYTDMarketShrink) x.dsGetPeriodShrinkDetails(target.FiscalYear, target.FiscalPeriod, "C")).Returns(dsTestData)
results = target.dtGetPeriodShrinks()
mockPer.Verify(Function(x As IYTDMarketShrink) x.dsGetPeriodShrinkDetails(target.FiscalYear, target.FiscalPeriod, "C"))
Assert.AreEqual(results.Rows.Count, 9)
The method being called in the business object:
Public Function dtGetPeriodShrinks() As DataTable
Try
GetReportBody()
Return dtTotalShrink
Catch ex As Exception
m_cLogging.LogError(ex, "CMarketShrinkPer.vb: dtGetPeroidShrinks")
Throw
End Try
End Function
And the method in question that is giving me trouble by going to another object, at the bottom of this code (objGeneral.GetInvalidStores()):
Private Sub GetReportBody()
Dim dsShrinkdetails As DataSet = Nothing
Dim dvShrink As DataView = Nothing
Dim drShrink As DataRow = Nothing
Dim intCurRgn As Int16
Dim intPrevRgn As Int16
Dim strRegion As String = String.Empty
Dim intCount As Integer
Dim intMarketCount As Integer
Dim strOrgHcy As String = String.Empty
Dim strInvalidStores As String = String.Empty
Dim drNewRow As DataRow = Nothing
Dim cGeneral As IGeneral = Nothing
Dim objGeneral As CGeneral = Nothing
Try
cGeneral = New cGeneralData(m_cLogging, m_cConfig)
objGeneral = New CGeneral(cGeneral, m_cLogging)
If Me.ReportType = m_strCURRENT_PERIOD_SHRINK Then
dsShrinkdetails = m_cMarketShrink.dsGetPeriodShrinkDetails(Me.FiscalYear, Me.FiscalPeriod, m_strCURRENT_PERIOD)
ElseIf Me.ReportType = m_strLAST_PERIOD_SHRINK Then
dsShrinkdetails = m_cMarketShrink.dsGetPeriodShrinkDetails(Me.FiscalYear, Me.FiscalPeriod, m_strLAST_PERIOD)
ElseIf Me.ReportType = m_strYTD_SHRINK Then
dsShrinkdetails = m_cMarketShrink.dsGetYTDShrinkDetails(Me.FiscalYear, Me.FiscalPeriod)
End If
If Not dsShrinkdetails Is Nothing AndAlso dsShrinkdetails.Tables.Count > 6 Then
m_dtShrinksales = dsShrinkdetails.Tables(Table.ShrinkSales)
m_dtStockLoss = dsShrinkdetails.Tables(Table.StockLoss)
m_dtThrowAways = dsShrinkdetails.Tables(Table.ThrowAways)
m_dtMarkDowns = dsShrinkdetails.Tables(Table.MarkDowns)
m_dtGasDriveOff = dsShrinkdetails.Tables(Table.GasDriveOff)
m_dtCashOverShort = dsShrinkdetails.Tables(Table.CashOverShort)
m_dtTotalShrinkAmt = dsShrinkdetails.Tables(Table.TotalShrink)
With objGeneral
.FiscalYear = Me.FiscalYear
.FiscalPeriod = Me.FiscalPeriod
End With
'To get invalid stores
strInvalidStores = objGeneral.GetInvalidStores()