1

我正在尝试对我的班级进行集成测试,以确保引发我希望引发的事件:

    'integration test not unit test
<TestMethod()>
Public Sub Change_Network_File_Causes_Event_To_Be_Raised()
    Dim EventCalled As Boolean
    Dim deployChk = New TRSDeploymentCheck("foo")
    deployChk._localFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TestFiles\SameLocalGUIDFile.txt")
    AddHandler deployChk.DeploymentNeeded, Sub() EventCalled = True
    deployChk.NetworkFileLocation = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TestFiles\SameNetGUIDFile.txt")
    ChangeNetworkFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TestFiles\SameNetGUIDFile.txt"))
    Assert.IsTrue(EventCalled)


End Sub

这是我在课堂上设置 FileSystemWatcher 对象的方法:

    Friend Property NetworkFileLocation As String
    Set(value As String)
        _netFileLoc = value
        If File.Exists(value) Then
            _watcher = New FileSystemWatcher(value.Replace(Path.GetFileName(value), String.Empty))
            _watcher.EnableRaisingEvents = True
            AddHandler _watcher.Changed, AddressOf OnNetworkFileChanged
        End If
    End Set
    Get
        Return _netFileLoc
    End Get
End Property  

Private Sub OnNetworkFileChanged(source As Object, e As FileSystemEventArgs)
    If IsDeploymentNeeded() Then RaiseEvent DeploymentNeeded()
End Sub

我在OneNetworkFileChange子中放置了一个断点。断点永远不会被击中。我已经验证了文件实际上在ChangeNetworkFile中被更改了我什至复制了代码(除了硬编码路径)并将其复制到我在单元测试期间运行的 Windows 应用程序中。它在我的 Windows 应用程序中工作。我在这里想念什么?

4

2 回答 2

1

经过一番测试终于弄明白了。好吧,上面的 EventCalled 从来都不是真的原因是因为测试的“windows 消息泵”被阻止了。该事件将被触发,但仅在测试完成后(当然这很晚)。那么如何解决呢?它有点乱,我不喜欢它,但我引用了 System.Windows.Forms.dll 并调用了Application.DoEvents()

        'integration test not unit test
<TestMethod()>
Public Sub Change_Network_File_Causes_Event_To_Be_Raised()
    Dim EventCalled As Boolean
    Dim deployChk = New TRSDeploymentCheck("foo")
    deployChk._localFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TestFiles\SameLocalGUIDFile.txt")
    AddHandler deployChk.DeploymentNeeded, Sub() EventCalled = True
    deployChk.NetworkFileLocation = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TestFiles\SameNetGUIDFile.txt")
    ChangeNetworkFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TestFiles\SameNetGUIDFile.txt"))
    Application.DoEvents()
    Assert.IsTrue(EventCalled)

End Sub

直到有人告诉我更好的方法,这似乎是解决方案。

于 2012-08-20T13:42:48.840 回答
0

可能它的过滤器(string.Empty)显然只查看没有扩展名的文件(这是一个假设)。

尝试"*.*"或类似的东西:

_watcher = New FileSystemWatcher(value.Replace(Path.GetFileName(value), string.Concat("*.", Path.GetExtension(value))))
于 2012-08-18T20:25:16.077 回答