https://github.com/accenture/autofixture.xunit2.automock
Autofixture auto-mocking for XUnit2 using a mocking library of your choice.
Science Score: 26.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
○CITATION.cff file
-
✓codemeta.json file
Found codemeta.json file -
✓.zenodo.json file
Found .zenodo.json file -
○DOI references
-
○Academic publication links
-
○Academic email domains
-
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (8.7%) to scientific vocabulary
Keywords
Repository
Autofixture auto-mocking for XUnit2 using a mocking library of your choice.
Basic Info
Statistics
- Stars: 23
- Watchers: 28
- Forks: 7
- Open Issues: 2
- Releases: 21
Topics
Metadata Files
README.md
Objectivity.AutoFixture.XUnit2.AutoMock
Accelerates preparation of mocked structures for unit tests under XUnit2 by configuring AutoFixture data generation to use a mocking library of your choice. Gracefully handles recursive structures by omitting recursions.
It provides the following mocking attributes:
- AutoMockData
- InlineAutoMockData
- MemberAutoMockData
Supported mocking libraries
| Mocking library | Corresponding NuGet package |
| ---------------------------------------------------------:|:--------------------------- |
| Moq | |
| NSubstitute |
|
| FakeItEasy |
|
Attributes
AutoMockData
Provides auto-generated data specimens generated by AutoFixture with a mocking library as an extension to xUnit.net's Theory attribute.
Arguments
- IgnoreVirtualMembers - disables generation of members marked as
virtual; by default set tofalse
Example
```csharp [Theory] [AutoMockData] public void GivenCurrencyConverterWhenConvertToPlnThenMustReturnCorrectConvertedAmount( string testCurrencySymbol, [Frozen] ICurrencyExchangeProvider currencyProvider, CurrencyConverter currencyConverter) { // Arrange Mock.Get(currencyProvider) .Setup(cp => cp.GetCurrencyExchangeRate(testCurrencySymbol)) .Returns(100M);
// Act
decimal result = currencyConverter.ConvertToPln(testCurrencySymbol, 100M);
// Assert
Assert.Equal(10000M, result);
} ```
InlineAutoMockData
Provides a data source for a Theory, with the data coming from inline values combined with auto-generated data specimens generated by AutoFixture with a mocking library.
Arguments
- IgnoreVirtualMembers - disables generation of members marked as
virtual; by default set tofalse
Example
```csharp [Theory] [InlineAutoMockData("USD", 3, 10, 30)] [InlineAutoMockData("EUR", 4, 20, 80)] public void GivenCurrencyConverterWhenConvertToPlnThenMustReturnCorrectConvertedAmount( string testCurrencySymbol, decimal exchangeRate, decimal currencyAmount, decimal expectedPlnAmount, [Frozen] ICurrencyExchangeProvider currencyProvider, CurrencyConverter currencyConverter) { // Arrange Mock.Get(currencyProvider) .Setup(cp => cp.GetCurrencyExchangeRate(testCurrencySymbol)) .Returns(exchangeRate);
// Act
decimal result = currencyConverter.ConvertToPln(testCurrencySymbol, currencyAmount);
// Assert
Assert.Equal(expectedPlnAmount, result);
} ```
MemberAutoMockData
Provides a data source for a Theory, with the data coming from one of the following sources:
- A static property
- A static field
- A static method (with parameters)
combined with auto-generated data specimens generated by AutoFixture with a mocking library.
The member must return something compatible with Enumerable<object[]> with the test data.
Caution: The property is completely enumerated by .ToList() before any test is run. Hence it should return independent object sets.
Arguments
- IgnoreVirtualMembers - disables generation of members marked as
virtual; by default set tofalse - ShareFixture - indicates whether to share a
fixtureacross all data items should be used or new one; by default set totrue
Example
csharp
public class CurrencyConverterFixture
{
public static IEnumerable<object[]> CurrencyConversionRatesWithResult()
{
return new List<object[]>
{
new object[] { "USD", 3M, 10M, 30M },
new object[] { "EUR", 4M, 20M, 80M }
};
}
}
```csharp [Theory] [MemberAutoMockData("CurrencyConversionRatesWithResult", MemberType = typeof(CurrencyConverterFixture))] public void GivenCurrencyConverterWhenConvertToPlnThenMustReturnCorrectConvertedAmount( string testCurrencySymbol, decimal exchangeRate, decimal currencyAmount, decimal expectedPlnAmount, [Frozen] ICurrencyExchangeProvider currencyProvider, CurrencyConverter currencyConverter) { // Arrange Mock.Get(currencyProvider) .Setup(cp => cp.GetCurrencyExchangeRate(testCurrencySymbol)) .Returns(exchangeRate);
// Act
decimal result = currencyConverter.ConvertToPln(testCurrencySymbol, currencyAmount);
// Assert
Assert.Equal(expectedPlnAmount, result);
} ```
IgnoreVirtualMembers
An attribute that can be applied to parameters in an AutoDataAttribute-driven Theory to indicate that the parameter value should not have virtual properties populated when the IFixture creates an instance of that type.
This attribute allows to disable the generation of members marked as virtual on a decorated type wheres IgnoreVirtualMembers arguments of mocking attributes mentioned above disable such a generation for all types created by IFixture.
Caution: Order is important! Applying IgnoreVirtualMembers attribute to the subsequent parameter makes preceding parameters of the same type to have virtual properties populated and the particular parameter with the following ones of the same type to have virtual properties unpopulated.
Example
csharp
public class User
{
public string Name { get; set; }
public virtual Address Address { get; set; }
}
```csharp [Theory] [AutoData] public void IgnoreVirtualMembersUsage( User firstUser, [IgnoreVirtualMembers] User secondUser, User thirdUser) { Assert.NotNull(firstUser.Name); Assert.NotNull(firstUser.Address);
Assert.NotNull(secondUser.Name);
Assert.Null(secondUser.Address);
Assert.NotNull(thirdUser.Name);
Assert.Null(thirdUser.Address);
} ```
CustomizeWith (borrowed from @devdigital)
An attribute that can be applied to parameters in an AutoDataAttribute-driven Theory to apply additional customization when the IFixture creates an instance of that type.
Arguments
- IncludeParameterType - indicates whether attribute target parameter
Typeshould included as a first argument when creating customization; by default set tofalse
Caution: Order is important! Applying CustomizeWith attribute to the subsequent parameter makes preceding parameters of the same type to be created without specified customization and the particular parameter with the specified customization.
Example
csharp
public class LocalDatesCustomization : ICustomization
{
public void Customize(IFixture fixture)
{
fixture.Register(() => LocalDate.FromDateTime(fixture.Create<DateTime>()));
}
}
```csharp [Theory] [InlineAutoMockData("USD")] [InlineAutoMockData("EUR")] public void GivenCurrencyConverterWhenConvertToPlnAtParticularDayThenMustReturnCorrectConvertedAmount( string testCurrencySymbol, [CustomizeWith(typeof(LocalDatesCustomization))] LocalDate day, [Frozen] ICurrencyExchangeProvider currencyProvider, CurrencyConverter currencyConverter) { // Arrange Mock.Get(currencyProvider) .Setup(cp => cp.GetCurrencyExchangeRate(testCurrencySymbol, day)) .Returns(100M);
// Act
decimal result = currencyConverter.ConvertToPln(testCurrencySymbol, 100M, day);
// Assert
Assert.Equal(10000M, result);
} ```
CustomizeWith<T>
A generic version of the CustomizeWith attribute has been introduced for ease of use. The same rules apply as for the non-generic version.
Example
```csharp public class EmptyCollectionCustomization : ICustomization { public EmptyCollectionCustomization(Type reflectedType) { this.ReflectedType = reflectedType; }
public Type ReflectedType { get; }
public void Customize(IFixture fixture)
{
var emptyArray = Array.CreateInstance(this.ReflectedType.GenericTypeArguments.Single(), 0);
fixture.Customizations.Add(
new FilteringSpecimenBuilder(
new FixedBuilder(emptyArray),
new ExactTypeSpecification(this.ReflectedType)));
}
} ```
csharp
public sealed class EmptyCollectionAttribute : CustomizeWithAttribute<EmptyCollectionCustomization>
{
public EmptyCollectionAttribute()
{
this.IncludeParameterType = true;
}
}
csharp
[Theory]
[AutoData]
public void CustomizeWithAttributeUsage(
IList<string> firstStore,
[EmptyCollection] IList<string> secondStore,
IList<string> thirdStore,
IList<int?> fourthStore)
{
Assert.NotEmpty(firstStore);
Assert.Empty(secondStore);
Assert.Empty(thirdStore);
Assert.NotEmpty(fourthStore);
}
Data filtering attributes
The following attributes helps narrowing down data generation to specific values or omitting certain values.
For these attributes to work, they must be used in conjunction with other data generation attributes.
They can be applied to simple types and collections.
Except
An attribute ensuring that values from outside the specified list will be generated.
Example
csharp
[Theory]
[AutoData]
public void ExceptAttributeUsage(
[Except(DayOfWeek.Saturday, DayOfWeek.Sunday)] DayOfWeek workday)
{
Assert.True(workday is >= DayOfWeek.Monday and <= DayOfWeek.Friday);
}
PickFromRange
An attribute ensuring that only values from specified range will be generated.
Example
csharp
[Theory]
[AutoData]
public void RangeAttributeUsage(
[PickFromRange(11, 19)] int teenagerAge)
{
Assert.True(teenagerAge is > 11 and < 19);
}
PickNegative
An attribute ensuring that only negative values will be generated.
Caution: It will throw exception when being used on unsupported type or on the one which does not accept negative values.
Example
csharp
[Theory]
[AutoData]
public void NegativeAttributeUsage(
[PickNegative] int negativeNumber)
{
Assert.True(negativeNumber < 0);
}
PickFromValues
An attribute ensuring that only values from the specified list will be generated.
Example
csharp
[Theory]
[AutoData]
public void ValuesAttributeUsage(
[PickFromValues(DayOfWeek.Saturday, DayOfWeek.Sunday)] HashSet<DayOfWeek> weekend)
{
var weekendDays = new[] { DayOfWeek.Saturday, DayOfWeek.Sunday };
Assert.Equivalent(weekendDays, weekend);
}
Tips and tricks
Fixture injection
You can inject same instance of IFixture to a test method by adding mentioned interface as an argument of test method.
csharp
[Theory]
[AutoMockData]
public void FixtureInjection(IFixture fixture)
{
Assert.NotNull(fixture);
}
IgnoreVirtualMembers issue
You should be aware that the CLR requires that interface methods be marked as virtual. Please look at the following example:
```csharp public interface IUser { string Name { get; set; } User Substitute { get; set; } }
public class User : IUser { public string Name { get; set; } public virtual User Substitute { get; set; } } ```
You can see than only Substitute property has been explicitly marked as virtual. In such situation the compiler will mark other properties as virtual and sealed. And finally AutoFixture will assign null value to those properties when option IgnoreVirtualMembers will be set to true.
csharp
[Theory]
[AutoMockData(IgnoreVirtualMembers = true)]
public void IssueWithClassThatImplementsInterface(User user)
{
Assert.Null(user.Name);
Assert.Null(user.Substitute);
}
Badges
| License | Code Coverage Map |
| :---------------------------------------------------------|:--------------------------- |
| |
|
Owner
- Name: Accenture
- Login: Accenture
- Kind: organization
- Website: https://accenture.github.io
- Repositories: 185
- Profile: https://github.com/Accenture
Accenture Github site
GitHub Events
Total
- Create event: 101
- Release event: 1
- Issues event: 2
- Watch event: 3
- Delete event: 98
- Issue comment event: 205
- Push event: 107
- Pull request review comment event: 16
- Pull request review event: 58
- Pull request event: 192
Last Year
- Create event: 101
- Release event: 1
- Issues event: 2
- Watch event: 3
- Delete event: 98
- Issue comment event: 205
- Push event: 107
- Pull request review comment event: 16
- Pull request review event: 58
- Pull request event: 192
Issues and Pull Requests
Last synced: 6 months ago
All Time
- Total issues: 2
- Total pull requests: 141
- Average time to close issues: about 2 months
- Average time to close pull requests: about 20 hours
- Total issue authors: 2
- Total pull request authors: 2
- Average comments per issue: 2.5
- Average comments per pull request: 1.79
- Merged pull requests: 53
- Bot issues: 0
- Bot pull requests: 112
Past Year
- Issues: 1
- Pull requests: 141
- Average time to close issues: N/A
- Average time to close pull requests: about 20 hours
- Issue authors: 1
- Pull request authors: 2
- Average comments per issue: 0.0
- Average comments per pull request: 1.79
- Merged pull requests: 53
- Bot issues: 0
- Bot pull requests: 112
Top Authors
Issue Authors
- piotrzajac (2)
- dependabot[bot] (2)
- PaJomantas (1)
- dalyIsaac (1)
Pull Request Authors
- dependabot[bot] (184)
- piotrzajac (38)
Top Labels
Issue Labels
Pull Request Labels
Packages
- Total packages: 3
-
Total downloads:
- nuget 637,188 total
-
Total dependent packages: 0
(may contain duplicates) -
Total dependent repositories: 0
(may contain duplicates) - Total versions: 66
- Total maintainers: 3
nuget.org: objectivity.autofixture.xunit2.automoq
Accelerates preparation of mocked structures for unit tests under xUnit2 by configuring AutoFixture data generation to use Moq. Gracefully handles recursive structures by omitting recursions.
- Homepage: https://github.com/Accenture/AutoFixture.XUnit2.AutoMock
- License: MIT
-
Latest release: 3.6.3
published 10 months ago
Rankings
Maintainers (2)
nuget.org: objectivity.autofixture.xunit2.autonsubstitute
Accelerates preparation of mocked structures for unit tests under xUnit2 by configuring AutoFixture data generation to use NSubstitute. Gracefully handles recursive structures by omitting recursions.
- Homepage: https://github.com/Accenture/AutoFixture.XUnit2.AutoMock
- License: MIT
-
Latest release: 3.6.3
published 10 months ago
Rankings
Maintainers (2)
nuget.org: objectivity.autofixture.xunit2.autofakeiteasy
Accelerates preparation of mocked structures for unit tests under xUnit2 by configuring AutoFixture data generation to use FakeItEasy. Gracefully handles recursive structures by omitting recursions.
- Homepage: https://github.com/Accenture/AutoFixture.XUnit2.AutoMock
- License: MIT
-
Latest release: 3.6.3
published 10 months ago
Rankings
Maintainers (2)
Dependencies
- Microsoft.NETFramework.ReferenceAssemblies 1.0.0 development
- AutoFixture 4.14.0
- AutoFixture.AutoFakeItEasy 4.14.0
- AutoFixture.Xunit2 4.14.0
- Castle.Core 4.4.1
- FakeItEasy 5.4.0
- JetBrains.Annotations 2020.1.0
- Microsoft.CodeAnalysis.FxCopAnalyzers 3.3.0
- StyleCop.Analyzers 1.1.118
- xunit.abstractions 2.0.3
- xunit.core 2.4.1
- xunit.extensibility.core 2.4.1
- xunit.extensibility.execution 2.4.1
- Microsoft.CodeAnalysis.FxCopAnalyzers 3.3.0 development
- Microsoft.NETFramework.ReferenceAssemblies 1.0.0 development
- StyleCop.Analyzers 1.1.118 development
- coverlet.msbuild 2.9.0 development
- xunit.runner.console 2.4.1 development
- xunit.runner.visualstudio 2.4.3 development
- AutoFixture 4.14.0
- AutoFixture.AutoFakeItEasy 4.14.0
- AutoFixture.Xunit2 4.14.0
- Castle.Core 4.4.1
- FakeItEasy 5.4.0
- FluentAssertions 5.10.3
- JetBrains.Annotations 2020.1.0
- Microsoft.NET.Test.Sdk 16.7.1
- xunit 2.4.1
- xunit.abstractions 2.0.3
- xunit.analyzers 0.10.0
- xunit.assert 2.4.1
- xunit.core 2.4.1
- xunit.extensibility.core 2.4.1
- xunit.extensibility.execution 2.4.1
- Microsoft.NETFramework.ReferenceAssemblies 1.0.0 development
- AutoFixture 4.14.0
- AutoFixture.AutoMoq 4.14.0
- AutoFixture.Xunit2 4.14.0
- Castle.Core 4.4.1
- JetBrains.Annotations 2020.1.0
- Microsoft.CodeAnalysis.FxCopAnalyzers 3.3.0
- Moq 4.14.5
- StyleCop.Analyzers 1.1.118
- xunit.abstractions 2.0.3
- xunit.core 2.4.1
- xunit.extensibility.core 2.4.1
- xunit.extensibility.execution 2.4.1
- Microsoft.CodeAnalysis.FxCopAnalyzers 3.3.0 development
- Microsoft.NETFramework.ReferenceAssemblies 1.0.0 development
- StyleCop.Analyzers 1.1.118 development
- coverlet.msbuild 2.9.0 development
- xunit.runner.console 2.4.1 development
- xunit.runner.visualstudio 2.4.3 development
- AutoFixture 4.14.0
- AutoFixture.AutoMoq 4.14.0
- AutoFixture.Xunit2 4.14.0
- Castle.Core 4.4.1
- FluentAssertions 5.10.3
- JetBrains.Annotations 2020.1.0
- Microsoft.NET.Test.Sdk 16.7.1
- Moq 4.14.5
- xunit 2.4.1
- xunit.abstractions 2.0.3
- xunit.analyzers 0.10.0
- xunit.assert 2.4.1
- xunit.core 2.4.1
- xunit.extensibility.core 2.4.1
- xunit.extensibility.execution 2.4.1
- Microsoft.NETFramework.ReferenceAssemblies 1.0.0 development
- AutoFixture 4.14.0
- AutoFixture.AutoNSubstitute 4.14.0
- AutoFixture.Xunit2 4.14.0
- Castle.Core 4.4.1
- JetBrains.Annotations 2020.1.0
- Microsoft.CodeAnalysis.FxCopAnalyzers 3.3.0
- NSubstitute 4.2.2
- StyleCop.Analyzers 1.1.118
- xunit.abstractions 2.0.3
- xunit.core 2.4.1
- xunit.extensibility.core 2.4.1
- xunit.extensibility.execution 2.4.1
- Microsoft.CodeAnalysis.FxCopAnalyzers 3.3.0 development
- Microsoft.NETFramework.ReferenceAssemblies 1.0.0 development
- StyleCop.Analyzers 1.1.118 development
- coverlet.msbuild 2.9.0 development
- xunit.runner.console 2.4.1 development
- xunit.runner.visualstudio 2.4.3 development
- AutoFixture 4.14.0
- AutoFixture.AutoNSubstitute 4.14.0
- AutoFixture.Xunit2 4.14.0
- Castle.Core 4.4.1
- FluentAssertions 5.10.3
- JetBrains.Annotations 2020.1.0
- Microsoft.NET.Test.Sdk 16.7.1
- NSubstitute 4.2.2
- xunit 2.4.1
- xunit.abstractions 2.0.3
- xunit.analyzers 0.10.0
- xunit.assert 2.4.1
- xunit.core 2.4.1
- xunit.extensibility.core 2.4.1
- xunit.extensibility.execution 2.4.1
- Microsoft.NETFramework.ReferenceAssemblies 1.0.0 development
- AutoFixture 4.14.0
- AutoFixture.Xunit2 4.14.0
- JetBrains.Annotations 2020.1.0
- Microsoft.CodeAnalysis.FxCopAnalyzers 3.3.0
- StyleCop.Analyzers 1.1.118
- xunit.abstractions 2.0.3
- xunit.core 2.4.1
- xunit.extensibility.core 2.4.1
- xunit.extensibility.execution 2.4.1
- Microsoft.CodeAnalysis.FxCopAnalyzers 3.3.0 development
- Microsoft.NETFramework.ReferenceAssemblies 1.0.0 development
- StyleCop.Analyzers 1.1.118 development
- coverlet.msbuild 2.9.0 development
- xunit.runner.console 2.4.1 development
- xunit.runner.visualstudio 2.4.3 development
- AutoFixture 4.14.0
- AutoFixture.Xunit2 4.14.0
- Castle.Core 4.4.1
- FluentAssertions 5.10.3
- JetBrains.Annotations 2020.1.0
- Microsoft.NET.Test.Sdk 16.7.1
- Moq 4.14.5
- xunit 2.4.1
- xunit.abstractions 2.0.3
- xunit.analyzers 0.10.0
- xunit.assert 2.4.1
- xunit.core 2.4.1
- xunit.extensibility.core 2.4.1
- xunit.extensibility.execution 2.4.1
- actions/checkout v3 composite
- actions/download-artifact v3 composite
- actions/upload-artifact v3 composite
- codecov/codecov-action v3 composite
- gittools/actions/gitversion/execute v0 composite
- gittools/actions/gitversion/setup v0 composite
- actions/checkout v3 composite
- github/codeql-action/analyze v2 composite
- github/codeql-action/init v2 composite
- actions/checkout v3 composite
- fossas/fossa-action main composite
- actions/checkout v3 composite
- gittools/actions/gitversion/execute v0 composite
- gittools/actions/gitversion/setup v0 composite
- codecov/codecov-action v3 composite
- ./.github/actions/materialize-signing-key * composite
- ./.github/actions/test-and-collect-coverage * composite
- actions/cache v3 composite
- actions/checkout v3 composite
- actions/upload-artifact v3 composite
- ./.github/actions/determine-next-version * composite
- actions/checkout v3 composite
- actions/download-artifact v3 composite
- actions/checkout v3 composite
- actions/checkout v3 composite
- github/codeql-action/upload-sarif v2 composite
- JetBrains/qodana-action v2023.3 composite
- actions/cache v4 composite
- actions/checkout v4 composite
- github/codeql-action/upload-sarif v3 composite