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

Passive Customization Attributes are ignored when used in combination with [Frozen] #637

Closed
sshushliapin opened this issue Jun 1, 2016 · 4 comments
Labels
Milestone

Comments

@sshushliapin
Copy link
Contributor

If a test method parameter is decorated with both [Substitute] and [Frozen] attributes, a concrete instance is created instead of a substitute. The following test fails:

[Theory, AutoNSubstituteData]
public void FreezeConcreteTypeSubstitute([Frozen, Substitute] ConcreteType ct1, ConcreteType ct2)
{
    Assert.Same(ct1, ct2);
    Assert.True(ct1.GetType().BaseType == typeof(ConcreteType)); // fails
    // A real instance is created instead of a mock.
}

The issue has been addressed in PR #627 but it was agreed that the solution is sub-optimal. To be implemented in a new PR.

@ploeh ploeh added the bug label Jun 2, 2016
@ploeh
Copy link
Member

ploeh commented Jun 2, 2016

@sergeyshushlyapin, while I haven't added the jump in label to this issue, you're still more than welcome to attempt to address it. You seem to be on top of things 👍

@sshushliapin
Copy link
Contributor Author

The issue is not about the [Substitute] attribute only. It's about all the Passive Attributes that require corresponding relays to create specimens (e.g. StringLengthAttributeRelay):

[Theory, AutoData]
public void StringLengthConstraintWorksWithoutFrozen([StringLength(3)]string str)
{
    Assert.True(str.Length == 3); // pass
}

[Theory, AutoData]
public void StringLengthConstraintFailsWithFrozen([Frozen][StringLength(3)]string str)
{
    Assert.True(str.Length == 3); // fails
}

The underlying reason is in the wrong assumption that in order to create a specimen only type is required. In the Glue Libraries specimens can be defined by types and Customization Attributes.

The FreezeOnMatchCustomization resolves specimens using type only which causes the issue:

var specimen = context.Resolve(this.TargetType);

@sshushliapin sshushliapin changed the title Unable to create a substitute of a test method parameter decorated with [Substritute] and [Frozen] attributes Passive Customization Attributes are ignored when used in combination with [Frozen] Jun 9, 2016
@zvirja
Copy link
Member

zvirja commented Jun 21, 2016

Just a hint: as a potential way to fix this issue is to create a custom ICustomAttributeProvider implementation and resolve it. That implementation should hide one attribute that we already processed. However, that could lead to large refactoring for existing code..

Update: disregard the comment above, I misunderstood the root of the issue.

@zvirja zvirja added this to the 4.0 milestone Aug 29, 2017
zvirja added a commit that referenced this issue Sep 1, 2017
…ttributes-v4

Passive Customization Attributes are ignored when used in combination with [Frozen] (#637, xUnit.net2, v4)
zvirja added a commit that referenced this issue Sep 11, 2017
…utes-nunit

Passive Customization Attributes are ignored when used in combination with [Frozen] (#637, NUnit2, v4)
zvirja added a commit that referenced this issue Sep 11, 2017
…utes-xunit.net

Passive Customization Attributes are ignored when used in combination with [Frozen] (#637, xUnit.net, v4)
zvirja added a commit that referenced this issue Sep 11, 2017
…utes-nunit3

Passive Customization Attributes are ignored when used in combination with [Frozen] (#637, NUnit3, v4)
@zvirja
Copy link
Member

zvirja commented Sep 11, 2017

Will be fixed in v4.

@zvirja zvirja closed this as completed Sep 11, 2017
@zvirja zvirja mentioned this issue Oct 17, 2017
28 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants