Details:
We were on 2013.2.611 and have upgraded to 2014.2.811.1.
We use TFS Build to run unit tests (under MSTest). I have already set up the code activity (including environmental settings) from the last version based on the JustMock guide and it was working with 100% success rate on the old version.
I updated the library to latest one from the new JustMock installation.
Builds completes and most of the JustMock related unit tests are failing with many similar error messages such as:
Telerik.JustMock.MockException: Arranging the return value of a constructor call is not supported.
Telerik.JustMock.MockException: Can not instantiate proxy of class: Could not find a parameterless constructor.
Are there any major changes between the versions that could be causing this? Something I forgot to update?
10 Answers, 1 is accepted
Since last year we've made numerous improvements to the library that strive to make writing unit tests more robust and less error prone. Unfortunately, this tightening often results in broken tests. Such tests were actually broken all this time without you knowing it.
The first exception: Arranging the return value of a constructor call is not supported.
It results from code like this:
Mock.Arrange(() =>
new
Fabrikam()).Returns(Mock.Create<Fabrikam>());
The other exception: Can not instantiate proxy of class: Could not find a parameterless constructor.
I'm not sure what change in JustMock causes this. Can you show me a short snippet of the test code together with the related class constructors?
I will do everything in my power to make the upgrade as painless as possible for you. I hope this helps.
Regards,
Stefan
Telerik
Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.
var mockgateway = Mock.Create<Gateway>(Behavior.Loose, Constructor.Mocked);
Telerik.JustMock.MockException: Can not instantiate proxy of class: DataAccess.Gateway.
Could not find a constructor that would match given arguments:
Telerik.JustMock.Constructor
Result StackTrace:
at Telerik.JustMock.Core.MocksRepository.Create(Type type, MockCreationSettings settings)
at Telerik.JustMock.MockBuilder.Create(MocksRepository repository, Type type, Object[] constructorArgs, Nullable`1 behavior, Type[] additionalMockedInterfaces, Nullable`1 mockConstructorCall, IEnumerable`1 additionalProxyTypeAttributes, List`1 supplementaryBehaviors, List`1 fallbackBehaviors, List`1 mixins, Expression`1 interceptorFilter)
at Telerik.JustMock.Mock.<>c__DisplayClass89`1.<Create>b__88()
at Telerik.JustMock.Core.ProfilerInterceptor.GuardInternal[T](Func`1 guardedAction)
at Telerik.JustMock.Mock.Create[T](Behavior behavior, Object[] args)
at ServiceLibrary.UnitTests...
Copy of constructor:
public class Gateway
{
public Gateway()
//: this(false)
{
} ... // Overloaded constructors follow
}
The correct form of that Create method overload is:
var mockgateway = Mock.Create<Gateway>(Constructor.Mocked, Behavior.Loose);
I am currently unable to track if and when the overload you're using existed. In any case, the fix is to just reorder the parameters in such calls to Mock.Create.
I would also urge you to check whether the default RecursiveLoose behavior is actually more suitable to your needs. In most cases it leads to shorter and more maintainable tests. Also, since it is the default, you can just create mocks with the shortest Create overload: Mock.Create<Gateway>().
Regards,
Stefan
Telerik
Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.
I'm happy that you've resolved the issues coming from the JustMock version upgrade. If you run into any other head-scratchers don't hesitate to contact us again.
Regards,
Stefan
Telerik
Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.
How bout this error:
​
System.MissingMethodException: Method 'AnyReason' not found on type AnalystMedicare.AnalystMedicare
The AnyReason method exists in the base class. Has there been a change around that. Here is the offending line:
Mock.NonPublic.Arrange<bool>(target, "AnyReason", ArgExpr.Ref(billLines[1])).IgnoreArguments().IgnoreInstance().Returns(true);
Yes, there have been a few bugfixes in the NonPublic API. The non-public API was rather lax with the argument list and didn't always correctly select the wanted overload when there were multiple methods with the same name but different arguments (in particular when 'ref' arguments were involved).
I surmise that the signature given to Arrange() doesn't match the AnyReason method's signature. Post the AnyReason method's signature here, so that I can check whether the arrangement and the signature match. In the next release of JustMock, errors in non-public arrangements (i.e. the MissingMethodException) will be accompanied by copy-and-paste snippets in the exception message of the correct arrangement statement for every member with the given name. This will make writing non-public arrangements a breeze!
Regards,
Stefan
Telerik
Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.
The method's argument is declared ByVal, so you shouldn't use ArgExpr.Ref there. Since you also have IgnoreArguments(), then it doesn't make sense to specify a concrete instance for the argument matcher. The correct form would then be:
Mock.NonPublic.Arrange<
bool
>(target,
"AnyReason"
, ArgExpr.IsAny<CBillRowExt>()).IgnoreInstance().Returns(
true
);
Alternatively, if you actually want to match the concrete instance, the correct form would be:
Mock.NonPublic.Arrange<
bool
>(target,
"AnyReason"
, billLines[1]).IgnoreInstance().Returns(
true
);
Essentially, the problem was caused by a misplaced ArgExpr.Ref.
Regards,
Stefan
Telerik
Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.