2011-06-22 65 views
1

以下C#代碼工作正常,並且測試按預期方式通過。在VB.NET中使用Rhino Mocks的Expect.Call模擬屬性

using NUnit.Framework; 
using Rhino.Mocks; 

namespace RhinoMocksTesting 
{ 
    public interface ITesting 
    { 
     string Test { get; } 
    } 

    [TestFixture] 
    public class MocksTest 
    { 

     [Test] 
     public void TestMockExpect() 
     { 
      var mocks = new MockRepository(); 
      var testMock = mocks.StrictMock<ITesting>(); 
      Expect.Call(testMock.Test).Return("testing"); 
      mocks.ReplayAll(); 
      Assert.AreEqual("testing", testMock.Test); 
     } 
    } 
} 

但是,在VB.NET中嘗試做同樣的事情甚至不會編譯!

Imports NUnit.Framework 
Imports Rhino.Mocks 

Public Interface ITesting 
    ReadOnly Property Test() As String 
End Interface 

<TestFixture()> _ 
Public Class MocksTest 

    <Test()> _ 
    Public Sub TestMockExpect() 
     Dim mocks = New MockRepository 
     Dim testMock = mocks.StrictMock(Of ITesting)() 
     Expect.Call(testMock.Test).Return("testing") 
     mocks.ReplayAll() 
     Assert.AreEqual("testing", testMock.Test) 
    End Sub 

End Class 

Expect.Call線產生下面的生成錯誤:「重載決策失敗,因爲沒有可訪問的‘期待’接受此數目的參數。」

在VB.NET中使用Expect.Call具有模擬屬性的正確方法是什麼?我見過幾篇文章說Rhino Mocks在VB10中工作得更好,但是我爲這個當前的項目堅持使用Visual Studio 2008。

回答

4

嘗試

Rhino.Mocks.Expect.Call(testMock.Test).Return("testing") 

Source

Now we switch to Rhino mocks 3.5 and see that we get an error on Expect saying that the signature is not correct. No worry this is because it is choosing the wrong Expect. It is namely trying to use the extension method there. Just add Rhino.Mocks. before the Expect and all is well again. Look how the imports doesn't do the same thing.

+0

哇。謝謝!我很高興這很簡單。 – CoderDennis

相關問題