Skip to content

Commit

Permalink
Merge branch 'stable' into release/v1.29
Browse files Browse the repository at this point in the history
  • Loading branch information
linkdotnet authored Jul 5, 2024
2 parents 8594d4d + 095add3 commit 7bbe533
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
35 changes: 35 additions & 0 deletions docs/site/docs/providing-input/inject-services-into-components.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,41 @@ Here is a test where the custom service provider is used via delegate:

[!code-csharp[](../../../samples/tests/xunit/CustomServiceProviderFactoryUsage.cs?start=25&end=29)]

## Using libraries like AutoMocker as fallback provider
It is possible to use libraries that automatically create mock services as fallback service providers. Here is an example implementation that utilizes the AutoMocker

```csharp
public class MockServiceProvider : IServiceProvider
{
private readonly AutoMocker _autoMocker = new AutoMocker();

public object? GetService(Type serviceType)
{
return _autoMocker.Get(serviceType);
}
}
```

> [!WARNING]
> An exception has to be made for `IComponentActivator`, if "Loose" mode is used. Otherwise, runtime exception can be thrown.
```csharp
public class MockServiceProvider : IServiceProvider
{
private readonly AutoMocker _autoMocker = new AutoMocker(MockBehavior.Loose);

public object? GetService(Type serviceType)
{
if (serviceType == typeof(IComponentActivator))
{
return null;
}

return _autoMocker.Get(serviceType);
}
}
```

## Further reading

A closely related topic is mocking. To learn more about mocking in bUnit, go to the <xref:test-doubles> page.
7 changes: 7 additions & 0 deletions docs/site/docs/test-doubles/emulating-ijsruntime.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ var moduleInterop = JSInterop.SetupModule("hello.js");
moduleInterop.SetupVoid("world");
```

When testing methods that return an `IJSObjectReference`, such as `await JsRuntime.InvokeAsync<IJSObjectReference>("SomeModule.GetInstance")`, the same process can be used with the identifier associated with the interoperation, configuring the `IJSObjectReference` in the same manner as a module.

```csharp
var objectReference = JSInterop.SetupModule(matcher => matcher.Identifier == "SomeModule.GetInstance");
objectReference.SetupVoid("world");
```

### Module Interop Mode

By default, a module Interop inherits the `Mode` setting from the root JSInterop in bUnit. However, you can override it explicitly and have it in a different mode from another module's Interop or the root JSInterop. Just set the `Mode` property, e.g.:
Expand Down

0 comments on commit 7bbe533

Please sign in to comment.