|
| 1 | +# MemberAutoMockData Attribute |
| 2 | + |
| 3 | +Provides a data source for a `Theory`, with the data coming from one of the following sources: |
| 4 | + |
| 5 | +- A static property |
| 6 | +- A static field |
| 7 | +- A static method (with parameters) |
| 8 | + |
| 9 | +combined with auto-generated data specimens generated by [AutoFixture](https://github.com/AutoFixture/AutoFixture) with a mocking library. |
| 10 | + |
| 11 | +The member must return something compatible with `Enumerable<object[]>` with the test data. |
| 12 | + |
| 13 | +**Caution:** The property is completely enumerated by .ToList() before any test is run. Hence it should return independent object sets. |
| 14 | + |
| 15 | +## Arguments |
| 16 | + |
| 17 | +- `IgnoreVirtualMembers` - disables generation of members marked as `virtual`; by default set to `false` |
| 18 | +- `ShareFixture` - indicates whether to share a `fixture` across all data items should be used or new one; by default set to `true` |
| 19 | + |
| 20 | +## Example |
| 21 | + |
| 22 | +```csharp |
| 23 | +public class CurrencyConverterFixture |
| 24 | +{ |
| 25 | + public static IEnumerable<object[]> CurrencyConversionRatesWithResult() |
| 26 | + { |
| 27 | + return new List<object[]> |
| 28 | + { |
| 29 | + new object[] { "USD", 3M, 10M, 30M }, |
| 30 | + new object[] { "EUR", 4M, 20M, 80M } |
| 31 | + }; |
| 32 | + } |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | +```csharp |
| 37 | +[Theory] |
| 38 | +[MemberAutoMockData("CurrencyConversionRatesWithResult", MemberType = typeof(CurrencyConverterFixture))] |
| 39 | +public void GivenCurrencyConverter_WhenConvertToPln_ThenMustReturnCorrectConvertedAmount( |
| 40 | + string testCurrencySymbol, |
| 41 | + decimal exchangeRate, |
| 42 | + decimal currencyAmount, |
| 43 | + decimal expectedPlnAmount, |
| 44 | + [Frozen] ICurrencyExchangeProvider currencyProvider, |
| 45 | + CurrencyConverter currencyConverter) |
| 46 | +{ |
| 47 | + // Arrange |
| 48 | + Mock.Get(currencyProvider) |
| 49 | + .Setup(cp => cp.GetCurrencyExchangeRate(testCurrencySymbol)) |
| 50 | + .Returns(exchangeRate); |
| 51 | + |
| 52 | + // Act |
| 53 | + decimal result = currencyConverter.ConvertToPln(testCurrencySymbol, currencyAmount); |
| 54 | + |
| 55 | + // Assert |
| 56 | + Assert.Equal(expectedPlnAmount, result); |
| 57 | +} |
| 58 | +``` |
0 commit comments