开发者

Mocked object set to true is being treated like it is false

开发者 https://www.devze.com 2023-03-20 06:16 出处:网络
I have a unit test (using typemock 5.4.5.0) that is testing a creation service.The creation service is passed in a validation service in its constructor.The validation service returns an object that h

I have a unit test (using typemock 5.4.5.0) that is testing a creation service. The creation service is passed in a validation service in its constructor. The validation service returns an object that has a boolean property (IsValid). In my unit test I am mocking the validation service call to return an instance that has IsValid set to true. The creation service has an if statement that checks the value of that property. When I run the unit test, the object returned from the validation service has its property set to true, but when the if statement is executed, it treats it as though it was false.

I can verify this by debugging the unit test. The object returned by the validation service does indeed have its IsValid property set to true, but it skips the body of my if statement entirely and goe开发者_如何学JAVAs to the End If.

Here is a link to the unit test itself - https://gist.github.com/1076372

Here is a link to the creation service function I am testing - https://gist.github.com/1076376

Does anyone know why the hell the IsValid property is true but is treated like it is false?

P.S. I have also entered this issue in TypeMock's support system, but I think I will probably get a quicker response here!


First, if possible, I'd recommend upgrading to the latest version of Typemock Isolator that you're licensed for. Each version that comes out, even minor releases, contains fixes for interesting edge cases that sometimes make things work differently. I've found upgrading sometimes fixes things.

Next, I see this line in your unit test:

Isolate.WhenCalled(() => validator.ValidateNewQuestionForExistingQuestionPool(new QuestionViewModel())).WillReturn(new Validation(true));

The red flag for me is the "new QuestionViewModel()" that's inside the "WhenCalled()" block.

Two good rules of thumb I always follow:

  1. Don't put anything in the WhenCalled() that you don't want mocked.
  2. If you don't care about the arguments, don't pass real arguments.

In this case, the first rule makes me think "I don't want the constructor for the QuestionViewModel mocked, so I shouldn't put it in there."

The second rule makes me consider whether the argument to the "ValidateNewQuestionForExistingPool" method really isn't important. In this case, it's not, so I'd pass null rather than a real object. If there's an overload you're specifically looking at, cast the null first.

Finally, sort of based on that first rule, I generally try not to inline my return values, either. That means I'd create the new Validation object before the Isolate call.

var validation = new Validator(true);
Isolate.WhenCalled(() => validator.ValidateNewQuestionForExistingQuestionPool(null)).WillReturn(validation);

Try that, see how it runs. You might also watch in the Typemock Tracer utility to see what's getting set up expectation-wise when you run your test to ensure additional expectations aren't being set up that you're not... expecting.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号