如何对.resx资源文件进行单元测试?



我有一个系统。在SubmitClick方法中使用的resx资源文件

Protected Sub SubmitClick(ByVal sender As Object, ByVal e As EventArgs)
  (...)
  If (... AndAlso ...) Then
      SetError(Resources.system.groupNoAdminTran)
  End If
End Sub

我的问题是,无论我如何尝试进行单元测试,当SetError被击中时,测试将失败:

"Could not load file or assembly 'App_GlobalResources' or one of its
 dependencies. The system cannot find the file specified."

有没有办法可以模拟Resources.system?

谢谢

这个解决方案可能不太优雅,但这是我最后使用的解决方案,请随意批评。

在参考资料中为每个需要的字符串手动创建一个属性:

Public Property GroupNoAdminTran() As String
    Get
        If String.IsNullOrEmpty(_groupNoAdminTran) Then
            _groupNoAdminTran = Resources.system.groupNoAdminTran
        End If
        Return _groupNoAdminTran
    End Get
    Set(ByVal value As String)
        _groupNoAdminTran = value
    End Set
End Property

可以这样使用:

Protected Sub SubmitClick(ByVal sender As Object, ByVal e As EventArgs)
  (...)
  If (... AndAlso ...) Then
      SetError(GroupNoAdminTran)
  End If
End Sub 
对于测试,一个简单的模拟就可以了:
_moqView.Setup(x => x.GroupNoAdminTran).Returns("GroupNoAdminTranTest");

,就这些。

最新更新