2011-12-05 94 views
1

我有一個在SubmitClick方法所使用的system.resx資源文件.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?

感謝

+0

什麼時候休息?你有錯誤嗎?你的單元測試沒有通過?你的單元測試看起來如何? –

+0

當SetError命中時,單元測試將失敗,並顯示「無法加載文件或程序集」App_GlobalResources「或其某個依賴項,系統找不到指定的文件。 – kooshka

+0

您的問題似乎是重複的: http://stackoverflow.com/questions/4153748/app-globalresources-not-loading-in-a-unit-test-case – Gilles

回答

0

這種解決方案可能不是優雅,但是我用到底,隨意批評之一。

手動創建的資源的每個需要字符串的屬性:

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"); 

就是這樣。我希望這有幫助。