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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃悰 AutoNSubstitute does not use ForPartsOf for abstract classes #1355

Open
rcdailey opened this issue Jun 27, 2022 · 1 comment
Open
Labels
Milestone

Comments

@rcdailey
Copy link

rcdailey commented Jun 27, 2022

I want to express my SUT as a parameter to my NUnit test case via AutoData. My SUT is an abstract class with virtual methods. The resulting proxy was created via Substitute.For<T>(), which means that methods I invoke on it do not actually call the real code in that abstract class. The solution to this for abstract classes to use Substitute.ForPartsOf<T>() instead.

Example:

public abstract class BaseCommand
{
    public virtual void DoThing()
    {
        // Do meaningful things
    }
}

In my test:

[TestFixture]
public class BaseCommandTest
{
    [Test, AutoMockData]
    public async Task All_directories_are_created(BaseCommand sut)
    {
        sut.DoThing(); // This does NOT execute code in `BaseCommand.DoThing()`!
    }
}

For completeness, my AutoMockData class is as follows:

public sealed class AutoMockDataAttribute : AutoDataAttribute
{
    public AutoMockDataAttribute()
        : base(NSubstituteFixture.Create)
    {
    }
}

public static class NSubstituteFixture
{
    public static Fixture Create()
    {
        var fixture = new Fixture
        {
            OmitAutoProperties = true
        };

        fixture.Customize(new AutoNSubstituteCustomization
        {
            ConfigureMembers = true
        });

        return fixture;
    }
}
@rcdailey rcdailey added the bug label Jun 27, 2022
@rcdailey
Copy link
Author

Rather than doing this automatically for abstract classes, which likely would be disruptive and break existing tests, perhaps it would be better to introduce a new PartialAttribute or something that can be used like this:

[Partial] BaseCommand sut

This would ultimately result in Substitute.ForPartsOf<T>().

@aivascu aivascu added this to the v5.0 milestone Dec 5, 2023
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

2 participants