chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes
Hi

I am new to the world of mocking and I am evaluating your product. I am running into the a problem when creating a mock for a web service proxy. I get a TypeMock.VerifyException stating that the parameter of the method been mocked is incorrect. I get the code working if I use simple parameters, but as soon as I use classes as parameters things start going wrong. Can you please help. See below for a simplified version of the code:


public class AuthenticationServiceGateway
{
public AuthenticateResponse Authenticate(AuthenticateRequest request)
{
// Do actual web service call and return AuthenticateResponse
}
}

public class AuthenticateRequest
{
public string UserId;
public string Password;

public AuthenticateRequest(string userId, string password)
{
this.UserId = userId;
this.Password = password;
}
}

public class AuthenticateResponse
{
public bool Authenticated;
public short DaysBeforePasswordExpires;

public AuthenticateResponse(bool authenticated, short daysBeforePasswordExpires)
{
this.Authenticated = authenticated;
this.DaysBeforePasswordExpires = daysBeforePasswordExpires;
}
}

public class AuthenticationService
{
public bool Authenticate(string userId, string password)
{
AuthenticationServiceGateway gateway = new AuthenticationServiceGateway();
AuthenticationResponse response = gateway.Authenticate(new AuthenticateRequest(userId, password));
if (!response.Authenticated)
{
// Do error handling
}
return true;
}
}

[TestFixture]
public class AuthenticationServiceFixture
{
private const string ValidUserName = "g902263";
private const string ValidPassword = "test14";

[Test]
public void TestAuthenticate()
{
MockManager.Init();
Mock serviceGatewayMock = MockManager.Mock(typeof(AuthenticationServiceGateway));
serviceGatewayMock.ExpectConstructor();

MyAuthenticateRequest request = new MyAuthenticateRequest(ValidUserName, ValidPassword);
MyAuthenticateResponse response = new MyAuthenticateResponse(true, 10);

serviceGatewayMock.ExpectAndReturn("Authenticate", response).Args(request);

AuthenticationService service = new AuthenticationService();
Assert.IsTrue(service.Authenticate(ValidUserName, ValidPassword));

MockManager.Verify();
}
}
asked by cjlotz (3k points)

3 Answers

0 votes
Hi,
Please tell me what error you are getting.

:idea: Tip: You can mock AuthenticateResponse using, MockManager.MockObject(typeof(AuthenticateResponse ));
and pass it by mock.Object as AuthenticateResponse

:idea: Tip: Using .Args(request) is the same as .Args(Check.IsEqual(request)). This uses Equals of the AuthenticateRequest type to check for equality.
So you can make sure that the Equals works correctly or create your own checker using the Custom Argument Checking
answered by scott (32k points)
0 votes
Thanks Scott

My newness to mocking was more the problem :oops: I solved it by using Custom Arguments Checking. Btw, I think you have a great product. One of the big selling points of your product is the fact that you actually have decent documentation about mocking included.
answered by cjlotz (3k points)
0 votes
Thanks alot,
I will tell the team :D
answered by scott (32k points)
...