![]() |
VOOZH | about |
dotnet add package Objectivity.AutoFixture.XUnit2.AutoNSubstitute --version 3.6.3
NuGet\Install-Package Objectivity.AutoFixture.XUnit2.AutoNSubstitute -Version 3.6.3
<PackageReference Include="Objectivity.AutoFixture.XUnit2.AutoNSubstitute" Version="3.6.3" />
<PackageVersion Include="Objectivity.AutoFixture.XUnit2.AutoNSubstitute" Version="3.6.3" />Directory.Packages.props
<PackageReference Include="Objectivity.AutoFixture.XUnit2.AutoNSubstitute" />Project file
paket add Objectivity.AutoFixture.XUnit2.AutoNSubstitute --version 3.6.3
#r "nuget: Objectivity.AutoFixture.XUnit2.AutoNSubstitute, 3.6.3"
#:package Objectivity.AutoFixture.XUnit2.AutoNSubstitute@3.6.3
#addin nuget:?package=Objectivity.AutoFixture.XUnit2.AutoNSubstitute&version=3.6.3Install as a Cake Addin
#tool nuget:?package=Objectivity.AutoFixture.XUnit2.AutoNSubstitute&version=3.6.3Install as a Cake Tool
👁 CI/CD
👁 codecov
👁 Mutation testing
👁 qodana
👁 License: MIT
👁 fossa
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:
| Mocking library | Corresponding NuGet package |
|---|---|
| Moq | 👁 AutoMoq 👁 Downloads |
| NSubstitute | 👁 AutoNSubstitute 👁 Downloads |
| FakeItEasy | 👁 AutoFakeItEasy 👁 Downloads |
Provides auto-generated data specimens generated by AutoFixture with a mocking library as an extension to xUnit.net's Theory attribute.
virtual; by default set to false[Theory]
[AutoMockData]
public void GivenCurrencyConverter_WhenConvertToPln_ThenMustReturnCorrectConvertedAmount(
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);
}
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.
virtual; by default set to false[Theory]
[InlineAutoMockData("USD", 3, 10, 30)]
[InlineAutoMockData("EUR", 4, 20, 80)]
public void GivenCurrencyConverter_WhenConvertToPln_ThenMustReturnCorrectConvertedAmount(
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);
}
Provides a data source for a Theory, with the data coming from one of the following sources:
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.
virtual; by default set to falsefixture across all data items should be used or new one; by default set to truepublic class CurrencyConverterFixture
{
public static IEnumerable<object[]> CurrencyConversionRatesWithResult()
{
return new List<object[]>
{
new object[] { "USD", 3M, 10M, 30M },
new object[] { "EUR", 4M, 20M, 80M }
};
}
}
[Theory]
[MemberAutoMockData("CurrencyConversionRatesWithResult", MemberType = typeof(CurrencyConverterFixture))]
public void GivenCurrencyConverter_WhenConvertToPln_ThenMustReturnCorrectConvertedAmount(
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);
}
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.
public class User
{
public string Name { get; set; }
public virtual Address Address { get; set; }
}
[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);
}
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.
Type should included as a first argument when creating customization; by default set to falseCaution: 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.
public class LocalDatesCustomization : ICustomization
{
public void Customize(IFixture fixture)
{
fixture.Register(() => LocalDate.FromDateTime(fixture.Create<DateTime>()));
}
}
[Theory]
[InlineAutoMockData("USD")]
[InlineAutoMockData("EUR")]
public void GivenCurrencyConverter_WhenConvertToPlnAtParticularDay_ThenMustReturnCorrectConvertedAmount(
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);
}
A generic version of the CustomizeWith attribute has been introduced for ease of use. The same rules apply as for the non-generic version.
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)));
}
}
public sealed class EmptyCollectionAttribute : CustomizeWithAttribute<EmptyCollectionCustomization>
{
public EmptyCollectionAttribute()
{
this.IncludeParameterType = true;
}
}
[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);
}
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.
An attribute ensuring that values from outside the specified list will be generated.
[Theory]
[AutoData]
public void ExceptAttributeUsage(
[Except(DayOfWeek.Saturday, DayOfWeek.Sunday)] DayOfWeek workday)
{
Assert.True(workday is >= DayOfWeek.Monday and <= DayOfWeek.Friday);
}
An attribute ensuring that only values from specified range will be generated.
[Theory]
[AutoData]
public void RangeAttributeUsage(
[PickFromRange(11, 19)] int teenagerAge)
{
Assert.True(teenagerAge is > 11 and < 19);
}
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.
[Theory]
[AutoData]
public void NegativeAttributeUsage(
[PickNegative] int negativeNumber)
{
Assert.True(negativeNumber < 0);
}
An attribute ensuring that only values from the specified list will be generated.
[Theory]
[AutoData]
public void ValuesAttributeUsage(
[PickFromValues(DayOfWeek.Saturday, DayOfWeek.Sunday)] HashSet<DayOfWeek> weekend)
{
var weekendDays = new[] { DayOfWeek.Saturday, DayOfWeek.Sunday };
Assert.Equivalent(weekendDays, weekend);
}
You can inject same instance of IFixture to a test method by adding mentioned interface as an argument of test method.
[Theory]
[AutoMockData]
public void FixtureInjection(IFixture fixture)
{
Assert.NotNull(fixture);
}
You should be aware that the CLR requires that interface methods be marked as virtual. Please look at the following example:
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.
[Theory]
[AutoMockData(IgnoreVirtualMembers = true)]
public void IssueWithClassThatImplementsInterface(User user)
{
Assert.Null(user.Name);
Assert.Null(user.Substitute);
}
| License | Code Coverage Map |
|---|---|
| 👁 FOSSA Status |
👁 Code Coverage Map |
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 was computed. net5.0-windows net5.0-windows was computed. net6.0 net6.0 was computed. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 was computed. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 netcoreapp2.0 was computed. netcoreapp2.1 netcoreapp2.1 was computed. netcoreapp2.2 netcoreapp2.2 was computed. netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 netstandard2.0 is compatible. netstandard2.1 netstandard2.1 is compatible. |
| .NET Framework | net461 net461 was computed. net462 net462 was computed. net463 net463 was computed. net47 net47 was computed. net471 net471 was computed. net472 net472 is compatible. net48 net48 is compatible. net481 net481 was computed. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen40 tizen40 was computed. tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
This package is not used by any NuGet packages.
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 3.6.3 | 11,633 | 4/25/2025 |
| 3.6.2 | 22,227 | 10/4/2024 |
| 3.6.1 | 11,641 | 12/12/2023 |
| 3.6.0 | 448 | 12/5/2023 |
| 3.5.1 | 32,608 | 10/13/2023 |
| 3.5.0 | 3,782 | 8/30/2023 |
| 3.4.1 | 1,629 | 4/3/2023 |
| 3.4.0 | 795 | 12/29/2022 |
| 3.3.3 | 96,028 | 9/16/2020 |
| 3.3.2 | 1,538 | 8/3/2020 |
| 3.3.1 | 2,157 | 10/29/2019 |
| 3.3.0 | 843 | 8/24/2019 |
| 3.2.0 | 912 | 3/5/2019 |
| 3.1.0 | 1,760 | 9/25/2018 |
| 3.0.67 | 1,033 | 9/17/2018 |
| 3.0.30 | 2,215 | 7/17/2018 |
| 3.0.25 | 1,500 | 7/17/2018 |
| 2.0.5 | 2,016 | 1/30/2018 |
| 2.0.0 | 1,677 | 1/9/2018 |