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

310 fallback service provider #315

Merged
merged 29 commits into from
Feb 8, 2021

Conversation

thopdev
Copy link
Contributor

@thopdev thopdev commented Feb 4, 2021

Pull request description

PR meta checklist

  • Pull request is targeting at DEV branch.
  • Pull request is linked to all related issues, if any.
  • I have read the CONTRIBUTING.md document.

Content checklist

  • My code follows the code style of this project and AspNetCore coding guidelines.
  • My change requires a change to the documentation.
    • I have updated the documentation accordingly.
  • I have updated the appropriate sub section in the CHANGELOG.md.
  • I have added, updated or removed tests to according to my changes.
    • All tests passed.

@thopdev
Copy link
Contributor Author

thopdev commented Feb 4, 2021

#315

@codecov
Copy link

codecov bot commented Feb 4, 2021

Codecov Report

Merging #315 (7aa4d7a) into dev (5b1ae91) will increase coverage by 0.11%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##              dev     #315      +/-   ##
==========================================
+ Coverage   81.40%   81.51%   +0.11%     
==========================================
  Files         126      126              
  Lines        3765     3777      +12     
  Branches      491      493       +2     
==========================================
+ Hits         3065     3079      +14     
+ Misses        541      540       -1     
+ Partials      159      158       -1     
Impacted Files Coverage Δ
src/bunit.core/TestServiceProvider.cs 94.36% <100.00%> (+1.14%) ⬆️
...it.core/Extensions/WaitForHelpers/WaitForHelper.cs 93.02% <0.00%> (+2.32%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5b1ae91...cad598e. Read the comment docs.

@egil egil linked an issue Feb 5, 2021 that may be closed by this pull request
@egil egil self-requested a review February 5, 2021 08:23
Copy link
Member

@egil egil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good so far. There are still a few test cases missing, as far as I can see. These are a few suggestions from the related issue:

  • when no fall back is added, the base service provider is used
  • when a fallback provider is added, the result from service provider is used when it is not null
  • when a fallback provider is added, it is called when the service provider returns null
  • if multipel fallbacks are added, only the latest is used
  • adding null as fallback throws argument null exception

@thopdev
Copy link
Contributor Author

thopdev commented Feb 5, 2021

Looks good so far. There are still a few test cases missing, as far as I can see. These are a few suggestions from the related issue:

  • when no fall back is added, the base service provider is used
  • when a fallback provider is added, the result from service provider is used when it is not null
  • when a fallback provider is added, it is called when the service provider returns null
  • if multipel fallbacks are added, only the latest is used
  • adding null as fallback throws argument null exception

Your right, thanks for the feedback, ill add them tonight / this weekend.

@thopdev
Copy link
Contributor Author

thopdev commented Feb 5, 2021

@egil I processed all you feedback but now one of the test fails.
The test contained this description:
// In some cases, the original assert wont work, since the sync context might not be idle,
/ which results in this order: First Third Second Fourth Fifth

Is this something i should look at?

@egil
Copy link
Member

egil commented Feb 6, 2021

@egil I processed all you feedback but now one of the test fails.
The test contained this description:
// In some cases, the original assert wont work, since the sync context might not be idle,
/ which results in this order: First Third Second Fourth Fifth

Is this something i should look at?

No, that is a race condition bug i am having a hard time reproducing locally, and have so far only seen here when running with GitHub actions. It is unrelated to this.

@thopdev
Copy link
Contributor Author

thopdev commented Feb 6, 2021

@egil I processed all you feedback but now one of the test fails.
The test contained this description:
// In some cases, the original assert wont work, since the sync context might not be idle,
/ which results in this order: First Third Second Fourth Fifth
Is this something i should look at?

No, that is a race condition bug i am having a hard time reproducing locally, and have so far only seen here when running with GitHub actions. It is unrelated to this

Then i think i got every thing done except for the changelog. Or is their anything else you would like to see improved?

Copy link
Member

@egil egil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think we are there. I am only nitpicking at this point. Did push an update to the docs section though, there was some docs build errors that I fixed, and added a bit more prosa to it.

I will also add the Packt editor who is helping with the docs to have him give the added docs a quick read through.

src/bunit.core/TestServiceProvider.cs Outdated Show resolved Hide resolved
src/bunit.core/TestServiceProvider.cs Outdated Show resolved Hide resolved
@egil
Copy link
Member

egil commented Feb 6, 2021

Hey @ventigrande,

Do you mind taking a look at the updated doc page in this PR. Here is a link with just the .md files visible in the Files Changed tab.

thopdev and others added 5 commits February 7, 2021 09:01
Co-authored-by: Egil Hansen <egil@assimilated.dk>
Co-authored-by: Egil Hansen <egil@assimilated.dk>
Co-authored-by: Egil Hansen <egil@assimilated.dk>
Co-authored-by: Egil Hansen <egil@assimilated.dk>
@egil
Copy link
Member

egil commented Feb 7, 2021

Everything looks good to me. Ill give wait with the merge until @ventigrande has had a chance to review the doc changes. Thanks for your contribution @thopdev!

@thopdev
Copy link
Contributor Author

thopdev commented Feb 7, 2021

@egil Sounds good. Thanks for all the help and feedback.

@thopdev
Copy link
Contributor Author

thopdev commented Feb 8, 2021

@ventigrande Thanks for the feedback.

@egil
Copy link
Member

egil commented Feb 8, 2021

Thank you @ventigrande.

One last thing @thopdev was needed. I updated the changelog file, let me know what you think.

@thopdev
Copy link
Contributor Author

thopdev commented Feb 8, 2021

Thank you @ventigrande.

One last thing @thopdev was needed. I updated the changelog file, let me know what you think.

Sounds good!

@egil egil merged commit 0c4b1a1 into bUnit-dev:dev Feb 8, 2021
@mwasson74
Copy link

mwasson74 commented Feb 9, 2023

@egil @thopdev I am new to bUnit but not to AutoFixture and XUnit. I could you please help me understand how to use/implement a FallbackServiceProvider using AutoFixture and Moq? I can't even seem to get the code from this Discussion to work. But maybe this code isn't needed now that you have allowed for a FallbackServiceProvider.

I did this but hopefully you tell me there's an easier way 🤞

  private class AutoFixtureServiceProvider : IServiceProvider
  {
    private readonly IFixture _fixture;

    private readonly Dictionary<Type, object> _mockedTypes = new();

    public AutoFixtureServiceProvider(IFixture fixture)
    {
      _fixture = fixture;
    }

    private object? GetMockedService(Type serviceType)
    {
      if (!_mockedTypes.TryGetValue(serviceType, out var service))
      {
        var createMethod =
          typeof(SpecimenFactory).GetMethod(nameof(SpecimenFactory.Create), new[] { typeof(ISpecimenBuilder) });
        var typedGeneric = createMethod?.MakeGenericMethod(serviceType);
        service = typedGeneric?.Invoke(null, new object?[] { _fixture });

        _mockedTypes.Add(serviceType, service!);
      }

      return service;
    }

    /// <inheritdoc />
    public object? GetService(Type serviceType)
    {
      return GetMockedService(serviceType);
    }
  }

Any guidance would be greatly appreciated.

Thanks!!

@thopdev
Copy link
Contributor Author

thopdev commented Feb 10, 2023

Hi @mwasson74 The service provider looks correct. Could you share one test class that shows bunit.

@mwasson74
Copy link

Thanks for getting back to me, @thopdev! Below, you'll find our test class with 1 bUnit test. It is working but I'll likely create an extension method to retrieve the Mock instead of doing Mock.Get(). But ideally, I could just have that mock instance given to me as a test method parameter like you did in your #307...

public class ClientSearchTests : TestContext
{
  private readonly IFixture _fixture;
  
  public ClientSearchTests()
  {
    _fixture = new Fixture();
    _fixture.Customize(new AutoMoqCustomization { ConfigureMembers = true });
    JSInterop.Mode = JSRuntimeMode.Loose;
    Services.AddMudServices();
    Services.AddFallbackServiceProvider(new AutoFixtureServiceProvider(_fixture));
  }

  [Theory, AutoMoqData]
  public void MyLittleTest(string clientCode)
  {
    //Actors
    var cut = RenderComponent<ClientSearch>(parameters =>
      parameters.Add(p => p.SearchCriteria, clientCode));
    var mockClient = Mock.Get(Services.GetService<IClientManagementClient>()!);

    //Acts
    cut.Find("button").Click();

    //Asserts
    mockClient?.Verify(x => x.GetClient, Times.Once);
  }
}

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

Successfully merging this pull request may close these issues.

Deeper integration with mocking frameworks
4 participants