Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SetupAllProperties with DefaultValue.Mock throws on concrete property type without parameterless constructor #149

Closed
ploeh opened this issue Jan 24, 2015 · 1 comment

Comments

@ploeh
Copy link
Sponsor

ploeh commented Jan 24, 2015

Steps to reproduce:

  1. Create a Mock of an interface with a writeable property, where the property type is a concrete type without a parameterless constructor.
  2. Set DefaultValue to DefaultValue.Mock
  3. Call SetupAllProperties

Here's an example:

public interface IHasAProperty
{
    Uri Url { get; set; }
}

[Fact]
public void Repro()
{
    var m = new Mock<IHasAProperty>();
    m.DefaultValue = DefaultValue.Mock;
    m.SetupAllProperties(); // Throws
}

Expected outcome

The mock's behaviour should now have 'property behaviour', and no exception should have been thrown.

Actual outcome

The call to SetupAllProperties throws an exception. Here's the exception thrown by the above example:

Test 'ClassLibrary1.Tests.Repro' failed: System.ArgumentException : A matching constructor for the given arguments was not found on the mocked type.
---- System.MissingMethodException : Constructor on type 'UriProxy8985c2aafdc84af0b77d8b098416d187' not found.
    at Moq.Mock`1.<InitializeInstance>b__0()
    at Moq.PexProtector.Invoke(Action action)
    at Moq.Mock`1.InitializeInstance()
    at Moq.Mock`1.get_Object()
    at Moq.Mock`1.GetObject()
    at Moq.MockDefaultValueProvider.ProvideDefault(MethodInfo member, Object[] arguments)
    at Moq.Mock.<>c__DisplayClass27.<SetupAllProperties>b__23()
    at Moq.PexProtector.Invoke(Action action)
    at Moq.Mock.SetupAllProperties(Mock mock)
    at Moq.Mock`1.SetupAllProperties()
    Tests.cs(18,0): at ClassLibrary1.Tests.Repro()
    ----- Inner Stack Trace -----
    at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, StackCrawlMark& stackMark)
    at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
    at System.Activator.CreateInstance(Type type, Object[] args)
    at Castle.DynamicProxy.ProxyGenerator.CreateClassProxy(Type classToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options, Object[] constructorArguments, IInterceptor[] interceptors)
    at Moq.Mock`1.<InitializeInstance>b__0()

If you comment out the line that sets DefaultValue to DefaultValue.Mock, not exception is thrown.

dougbu added a commit to aspnet/AspNetWebStack that referenced this issue Aug 25, 2016
- work around devlooped/moq#129 and devlooped/moq#212; Moq creates proxies for all `interface` methods
 - e.g. set up `IDisposable.Dispose()` calls
 - related bug devlooped/moq#212 means we can't use `MockBehavior.Strict` in some cases
  - especially when method returns a value and therefore can't be set up to call base
  - `CallBase = true` is often required in these cases
- work around devlooped/moq#149; `DefaultValue.Mock` restrictions
 - where necessary, explicitly set up members instead
- work around odd failures in `JsonResultTest` using `SetupSet()` on deep properties
 - use `MockBehavior.Strict` in `NullContentIsNotOutput()` to confirm `Write()` is not called
- handle less-predictable proxy type names
dougbu added a commit to aspnet/AspNetWebStack that referenced this issue Aug 26, 2016
- #11 (3 of 4)
- work around devlooped/moq#129 and devlooped/moq#212; Moq creates proxies for all `interface` methods
 - e.g. set up `IDisposable.Dispose()` calls
 - related bug devlooped/moq#212 means we can't use `MockBehavior.Strict` in some cases
  - especially when method returns a value and therefore can't be set up to call base
  - `CallBase = true` is often required in these cases
- work around devlooped/moq#149; `DefaultValue.Mock` restrictions
 - where necessary, explicitly set members up instead
- work around odd failures in `JsonResultTest` using `SetupSet()` on deep properties
 - use `MockBehavior.Strict` in `NullContentIsNotOutput()` to confirm `Write()` is not called
- handle less-predictable proxy type names
@stakx
Copy link
Contributor

stakx commented Jun 20, 2017

Hi @ploeh, just for the record, the issue you reported is no longer reproducible starting with Moq version 4.5.19. This must have been fixed by #245.

@stakx stakx closed this as completed Jun 20, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants