Autofixture fixture setup questions
See original GitHub issueMy plan is setup kind of a builder pattern for S3, to perform various ops (put,get,list,delete etc…)
`using Amazon.S3; using Amazon.S3.Model; using AutoFixture; using AutoFixture.AutoMoq; using Moq; using System; using System.Collections.Generic; using System.Text; using System.Threading;
namespace PdfUploder.Tests.UnitTests { public class StorageBuilder { private IFixture _fixture; private readonly Mock<AmazonS3Client> _client; public StorageBuilder() { _fixture = new Fixture().Customize(new AutoMoqCustomization()); _client = _fixture.Freeze<Mock<AmazonS3Client>>(); }
public StorageBuilder WithPut()
{
var res = _fixture.Create<PutObjectResponse>();
Func<PutObjectRequest, CancellationToken, PutObjectResponse> response
= (p, c) => { return res; };
_client
.Setup(x => x.PutObjectAsync(
It.IsAny<PutObjectRequest>(),
It.IsAny<CancellationToken>()))
.ReturnsAsync(response);
return this;
}
public Mock<AmazonS3Client> Build()
{
return _client;
}
}
}`
Q1. when i run this command
var res = _fixture.Create<PutObjectResponse>();
if get this error
AutoFixture.ObjectCreationExceptionWithPath: 'AutoFixture was unable to create an instance from Amazon.S3.RequestCharged because creation unexpectedly failed with exception. Please refer to the inner exception to investigate the root cause of the failure
Q2. how to get fully prepared object of Microsoft.AspNetCore.Http -->IFormFile
using this attribute, getting an object
public AutoMoqDataAttribute() : base(() => new Fixture().Customize(new AutoMoqCustomization() { ConfigureMembers = true })) { }
FileName ContentType ContentDisposition
but everything is coming null
Issue Analytics
- State:
- Created 2 years ago
- Comments:14
Top GitHub Comments
I think you dont get what I wrote.
Your Api suggests that the User class can have any kind of class implementing the interface IUserProfile. AutoFixture only sees the property Profile of type IUserProfile with a getter and a setter. So AutoFixture tries to create this type.
Normally AutoFixture cannot create interfaces, but due to the AutoMoqCustomization AutoFixture creates a Mock implementing this interface.
The error is in your SDK. Either the property Profile should be of type UserProfile or your code to get it back should not require your own class. What is the point of having this interface, if you dont use it?
Of course you can create a specimenbuilder responsible for IUserProfile returning a UserProfile.
But more or less the behavior of AutoFixture is logic. A property of type IUserProfile is filled with a class implementing the interface. There is no way that AutoFixture can figure out without help that you dont want any class implementing the interface, but your own.
public sealed class User : Resource, IUser, IResource { public User(); public IUserProfile Profile { get; set; }
yes - i want to IUserProfile property autopouplated with default value
correct me - is this correct approch?
fixture.Inject(fixture.Build<User>() .With(x=>x.Id,“some-id”) .With(x => x.Profile, new UserProfile { }).Create());
otherwide profile will be always null in user param
[Theory, AutoMoqData] public async Task CreateUser(User user,
{ //user.Profile always null user.Profile.Email …
}