Created
January 5, 2018 17:56
-
-
Save JaykeOps/ea7b57658129346848e8fde1fb29109f to your computer and use it in GitHub Desktop.
AutoFixture + AutoMoq - Using Custom Attribute to Generate TestData
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using AutoFixture; | |
using AutoFixture.AutoMoq; | |
using AutoFixture.Xunit2; | |
namespace AutoFixtureSandbox.Tests | |
{ | |
public class AutoMoqDataAttribute : AutoDataAttribute | |
{ | |
public AutoMoqDataAttribute() : base(() => | |
{ | |
var fixture = new Fixture(); | |
fixture.Customize(new AutoConfiguredMoqCustomization()); | |
return fixture; | |
}){} | |
} | |
} | |
--- | |
using AutoFixture.Xunit2; | |
using Moq; | |
using Xunit; | |
namespace AutoFixtureSandbox.Tests | |
{ | |
public class AutoFixtureLearningTests | |
{ | |
[Theory] | |
[AutoMoqData] | |
public void CanSetTestDoubleWithAutoFixtureAutoMoq([Frozen] Mock<IEngine> mockEngine, Car sut) | |
{ | |
sut.Start(); | |
Assert.NotNull(sut.EngineModel); | |
mockEngine.Verify(engine => engine.Start(), Times.Once); | |
} | |
} | |
public class Car | |
{ | |
private readonly string model; | |
private readonly string make; | |
private readonly int productionYear; | |
private readonly IEngine engine; | |
public Car(string model, string make, int productionYear, IEngine engine) | |
{ | |
this.model = model; | |
this.make = make; | |
this.productionYear = productionYear; | |
this.engine = engine; | |
} | |
public string Model => model; | |
public string Make => make; | |
public int ProductionYear => productionYear; | |
public string EngineModel => engine.Model; | |
public void Start() => engine.Start(); | |
} | |
public interface IEngine | |
{ | |
string Model { get; } | |
void Start(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment