유닛 테스트를 원하는 기존 코드가 있습니다. 나는 처음 MOQ 테스트를 생성하지만 다음과 같은 예외가 점점 오전 :모의 (mock)에 대한 모든 호출에는 해당 설정이 있어야합니다.
Moq.MockException : IConnection.SendRequest (ADF.Messaging.Contract.ConfigServer.GetDataVersionRequest) 호출이 엄격한 모의 행동에 실패했습니다. mock의 모든 호출에는 해당 설정이 있어야합니다. 코드의
중요 조각 :
클래스의 속성 :
이Public Property Connection() As IConnection
Get
Return _connection
End Get
Set(ByVal value As IConnection)
_connection = value
End Set
End Property
테스트해야하는 방법 : (_connection)는 TCP 소켓을 만드는 실제로 클래스 내가 원하는 그 속성을 모의 해 SendRequest가 내가 원하는 것을 반환하도록하십시오.
Public Function GetVersion(ByVal appID As Contract.ApplicationID) As Contract.DataVersion
EnsureConnected()
Dim req As GetDataVersionRequest = New GetDataVersionRequest(appID)
Dim reply As CentralServiceReply = _connection.SendRequest(req) //code I want to mock
Utils.Check.Ensure(TypeOf reply Is GetDataVersionReply, String.Format("Unexpected type: {0}, expected GetDataVersionReply!", reply.GetType()))
Dim version As Contract.DataVersion = CType(reply, GetDataVersionReply).Version
version.UpgradeOwners()
If (Not version.IsSupported) Then
Return Contract.DataVersion.UNSUPPORTED
End If
Return version
End Function
시험 방법 :
[TestMethod]
public void TestMethod2()
{
Contract.CentralServiceRequest req = new Contract.ConfigServer.GetDataVersionRequest(new ApplicationID("AMS", "QA"));
DataVersion v = new DataVersion();
v.AppVersion = "16";
CentralServiceReply reply = new GetDataVersionReply(v);
var ConnectionMock = new Mock<IConnection>(MockBehavior.Strict);
ConnectionMock.Setup(f => f.SendRequest(req)).Returns(reply);
var proxy = new ConfigServerProxy(new ApplicationID("AMS", "QA"), "ws23545", 8001);
proxy.Connection = ConnectionMock.Object; //assign mock object
DataVersion v2 = proxy.GetVersion(new ApplicationID("AMS", "QA"));
Assert.AreEqual(v.AppVersion, v2.AppVersion);
}
나는 우리가 오류가 _connection.SendRequest proxy.GetVersion이 라인에 실행될 때 볼 단위 테스트를 디버깅 할 때. 또한 watch 윈도우에서 변수 (_connection)를 볼 때 moq 객체라는 것을 알 수 있습니다. 그래서 나는 부동산 할당이 잘되었다고 생각합니다.
어디서 잘못 본 사람이 있습니까?
차가워 져서이 정보로 업데이트하려고합니다 : – Josh
항상 발생합니다. –
반환 할 항목은 반환 (응답)입니다. 또한 수신 된 변수를 처리하는보다 정교한 방법이 있습니다 (예 : 요청에 일부 속성이 설정되어 있어야 함). 수행 할 수있는 작업을 보려면 설명서/자습서를 확인하십시오. –