chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes
I've written some tests, which all pass when run individually, yet fail when run as a collection through NUnit. I've made sure that the following are declared in the setup and teardown functions. Any suggestions?

   <SetUp()> _
    Protected Sub TestSetUp()
      MockManager.Init()  
    End Sub

    <TearDown()> _
    Protected Sub TestShutdown()
      MockManager.Verify()
      MockManager.ClearAll()
    End Sub


A sample test that fails is as follows:

<Test()> _
     Public Sub TestMaxValue()

      'Constants
      Const FIELD_FRIENDLY_NAME As String = "YearsOfService"

      Const MAX_VALUE As Integer = 10
      Const TOO_BIG_VALUE As Integer = 15
      Const CORRECT_VALUE As Integer = 8

      Const RULE_TYPE As ValidationHelper.RuleType = ValidationHelper.RuleType.MaxValue

      'Expectations
      Using rec As RecordExpectations = RecorderManager.StartRecording()

        rec.ExpectAndReturn(TempBOID.GetNext, -304).RepeatAlways()
        rec.ExpectAndReturn(Database.PredatorConnection, CONNECTION_STRING)

        Dim Cn As SqlConnection = New SqlConnection(CONNECTION_STRING)
        rec.MockStaticConstructors()
        Cn.Open()
        Cn.Close()

        Dim Cm As SqlCommand = New SqlCommand()
        Cm.CommandText = SpNames.GET_VALIDATION_INFOLIST
        rec.CheckArguments()
        Cm.Parameters.AddWithValue(SQLParam.BUSINESS_OBJECT_FULL_NAME, BUSINNESS_OBJECT)

        Dim dr As ExtendedSafeDataReader = New ExtendedSafeDataReader(Cm.ExecuteReader())
        rec.MockStaticConstructors()
        rec.MockAllInstances = True

        rec.ExpectAndReturn(dr.Read(), True)

        dr.GetInt32(FieldNames.RULE_ID)
        rec.Return(EXISTING_ID).CheckArguments()

        dr.GetString(FieldNames.FIELD_NAME)
        rec.Return(FIELD_FRIENDLY_NAME).CheckArguments()

        'Code removed  to shorten post

        rec.ExpectAndReturn(dr.Read(), False)
        dr.Dispose()


      End Using

      'Assert
      Dim NewProject As Project = Project.NewProject()
      Assert.IsTrue(NewProject.IsValid)

    End Sub

    <Test()> _
       Public Sub TestNoRules()

      'Expectations
      Using rec As RecordExpectations = RecorderManager.StartRecording()

        rec.ExpectAndReturn(TempBOID.GetNext, RULE_ID).RepeatAlways()
        rec.ExpectAndReturn(Database.PredatorConnection, CONNECTION_STRING)

        Dim Cn As SqlConnection = New SqlConnection(CONNECTION_STRING)
        rec.MockStaticConstructors()
        Cn.Open()
        Cn.Close()

        Dim Cm As SqlCommand = New SqlCommand()
        Cm.CommandText = SpNames.GET_VALIDATION_INFOLIST
        rec.CheckArguments()

        Cm.Parameters.AddWithValue(SQLParam.BUSINESS_OBJECT_FULL_NAME, BUSINNESS_OBJECT)

        Dim dr As ExtendedSafeDataReader = New ExtendedSafeDataReader(Cm.ExecuteReader())
        rec.MockStaticConstructors()


        rec.ExpectAndReturn(dr.Read(), False)
        dr.Dispose()


      End Using

      'Assert
      Dim NewProject As Project = Project.NewProject()

      Assert.AreEqual(RULE_ID, NewProject.Id)
      Assert.IsTrue(NewProject.IsValid)


    End Sub
[/code]
asked by Marjon (600 points)

1 Answer

0 votes
Hi,
I have tried it in house and it works like a charm.
:idea: Just a tip. You should have the MockManager.Verify within the test method and not in the teardown. Leave the ClearAll in the teardown.

:arrow: About your problem, there are probably leftovers in your test, i.e. some Shared fields are set, that then change the way the code works.
If you would like please run the tracer to see how your mocks are running, you can also send us the trace log so that we can check it.
answered by scott (32k points)
...