0
經過3個多小時的挫折之後,我決定詢問。MSTest PrivateType.InvokeStatic()拋出MissingMethodException
我有這樣簡單的類將被測試:
public class Organizer {
private static bool Bongo(String p) {
return p != null && p.Length >= 5;
}
private static int ValidateArgs(String[] args) {
return args.Length;
}
}
和我使用MSTest的測試靜態方法有這樣的代碼:在TestBongo從第一斷言
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
public class OrganizerTest {
[TestMethod]
public void TestBongo() {
PrivateType psOrganizers = new PrivateType(typeof(Organizer));
// This one works fine.
Assert.IsTrue(Convert.ToBoolean(psOrganizers.InvokeStatic("Bongo", "ABCDEFG")));
// Fails with: "System.MissingMethodException: Method 'Organizer.Bongo' not Found.
Assert.IsFalse(Convert.ToBoolean(psOrganizers.InvokeStatic("Bongo", null)));
}
[TestMethod]
public void TestBodo() {
PrivateType psOrganizers = new PrivateType(typeof(Organizer));
String[] fakeArray = new String[] { "Pizza" };
// Fails with: "System.MissingMethodException: Method 'Organizer.ValidateArgs' not Found.
int result1 = Convert.ToInt32(psOrganizers.InvokeStatic("ValidateArgs", fakeArray));
// Fails with: "System.MissingMethodException: Method 'Organizer.ValidateArgs' not Found.
int result2 = Convert.ToInt32(psOrganizers.InvokeStatic("ValidateArgs", "A", "B", "C", "D"));
// Fails with: "System.MissingMethodException: Method 'Organizer.ValidateArgs' not Found.
int result3 = Convert.ToInt32(psOrganizers.InvokeStatic("ValidateArgs", new Object[] { "A", "B", "C", "D" }));
Assert.IsTrue(result1 == 1 && result2 == 4 && result3 == 4);
}
}
除了,每隔由於MissingMethodException而導致Assert失敗。有很多InvokeStatic()重載,我懷疑它可能是編譯器沒有選擇我期望的那種情況。
順便說一句,請不要告訴我不要測試私有方法。我不是在尋找辯論。 :)
謝謝。
謝謝,邁克。就是這樣。 –