Skip to content

Commit ede43b8

Browse files
committed
Merge branch 'stable'
2 parents 425c90a + 8c9a911 commit ede43b8

File tree

3 files changed

+72
-27
lines changed

3 files changed

+72
-27
lines changed

CHANGELOG.md

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ All notable changes to **bUnit** will be documented in this file. The project ad
66

77
## [Unreleased]
88

9+
## [1.29.5] - 2024-07-05
10+
911
### Fixed
1012

1113
- CI build changes to force running verification on x64 based AMD CPUs.
@@ -521,32 +523,32 @@ List of now removed features.
521523
1. Add the following packages to your test project: `Serilog`, `Serilog.Extensions.Logging`, and `Serilog.Sinks.XUnit`.
522524
2. Add the following class/extension method to your test project (which replicates the signature of the removed `AddXunitLogger` method):
523525
524-
```csharp
525-
using Microsoft.Extensions.DependencyInjection;
526-
using Microsoft.Extensions.Logging;
527-
using Serilog;
528-
using Serilog.Events;
529-
using Xunit.Abstractions;
530-
531-
namespace Bunit
532-
{
533-
public static class ServiceCollectionLoggingExtensions
534-
{
535-
public static IServiceCollection AddXunitLogger(this IServiceCollection services, ITestOutputHelper outputHelper)
536-
{
537-
var serilogLogger = new LoggerConfiguration()
538-
.MinimumLevel.Verbose()
539-
.WriteTo.TestOutput(outputHelper, LogEventLevel.Verbose)
540-
.CreateLogger();
541-
542-
services.AddSingleton<ILoggerFactory>(new LoggerFactory().AddSerilog(serilogLogger, dispose: true));
543-
services.AddSingleton(typeof(ILogger<>), typeof(Logger<>));
544-
545-
return services;
546-
}
547-
}
548-
}
549-
```
526+
```csharp
527+
using Microsoft.Extensions.DependencyInjection;
528+
using Microsoft.Extensions.Logging;
529+
using Serilog;
530+
using Serilog.Events;
531+
using Xunit.Abstractions;
532+
533+
namespace Bunit
534+
{
535+
public static class ServiceCollectionLoggingExtensions
536+
{
537+
public static IServiceCollection AddXunitLogger(this IServiceCollection services, ITestOutputHelper outputHelper)
538+
{
539+
var serilogLogger = new LoggerConfiguration()
540+
.MinimumLevel.Verbose()
541+
.WriteTo.TestOutput(outputHelper, LogEventLevel.Verbose)
542+
.CreateLogger();
543+
544+
services.AddSingleton<ILoggerFactory>(new LoggerFactory().AddSerilog(serilogLogger, dispose: true));
545+
services.AddSingleton(typeof(ILogger<>), typeof(Logger<>));
546+
547+
return services;
548+
}
549+
}
550+
}
551+
```
550552
551553
- The `bunit.xunit` package has been removed, since it is no longer needed (there is no code left in it).
552554
@@ -1374,7 +1376,8 @@ The latest version of the library is availble on NuGet:
13741376
- **Wrong casing on keyboard event dispatch helpers.**
13751377
The helper methods for the keyboard events was not probably cased, so that has been updated. E.g. from `Keypress(...)` to `KeyPress(...)`.
13761378

1377-
[unreleased]: https://github.com/bUnit-dev/bUnit/compare/v1.28.9...HEAD
1379+
[unreleased]: https://github.com/bUnit-dev/bUnit/compare/v1.29.5...HEAD
1380+
[1.29.5]: https://github.com/bUnit-dev/bUnit/compare/v1.28.9...1.29.5
13781381
[1.28.9]: https://github.com/bUnit-dev/bUnit/compare/v1.27.17...v1.28.9
13791382
[1.27.17]: https://github.com/bUnit-dev/bUnit/compare/v1.26.64...1.27.17
13801383
[1.26.64]: https://github.com/bUnit-dev/bUnit/compare/v1.25.3...v1.26.64

docs/site/docs/providing-input/inject-services-into-components.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,41 @@ Here is a test where the custom service provider is used via delegate:
8080

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

83+
## Using libraries like AutoMocker as fallback provider
84+
It is possible to use libraries that automatically create mock services as fallback service providers. Here is an example implementation that utilizes the AutoMocker
85+
86+
```csharp
87+
public class MockServiceProvider : IServiceProvider
88+
{
89+
private readonly AutoMocker _autoMocker = new AutoMocker();
90+
91+
public object? GetService(Type serviceType)
92+
{
93+
return _autoMocker.Get(serviceType);
94+
}
95+
}
96+
```
97+
98+
> [!WARNING]
99+
> An exception has to be made for `IComponentActivator`, if "Loose" mode is used. Otherwise, runtime exception can be thrown.
100+
101+
```csharp
102+
public class MockServiceProvider : IServiceProvider
103+
{
104+
private readonly AutoMocker _autoMocker = new AutoMocker(MockBehavior.Loose);
105+
106+
public object? GetService(Type serviceType)
107+
{
108+
if (serviceType == typeof(IComponentActivator))
109+
{
110+
return null;
111+
}
112+
113+
return _autoMocker.Get(serviceType);
114+
}
115+
}
116+
```
117+
83118
## Further reading
84119

85120
A closely related topic is mocking. To learn more about mocking in bUnit, go to the <xref:test-doubles> page.

docs/site/docs/test-doubles/emulating-ijsruntime.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,13 @@ var moduleInterop = JSInterop.SetupModule("hello.js");
8888
moduleInterop.SetupVoid("world");
8989
```
9090

91+
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.
92+
93+
```csharp
94+
var objectReference = JSInterop.SetupModule(matcher => matcher.Identifier == "SomeModule.GetInstance");
95+
objectReference.SetupVoid("world");
96+
```
97+
9198
### Module Interop Mode
9299

93100
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.:

0 commit comments

Comments
 (0)