question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Autofixture fixture setup questions

See original GitHub issue

My 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:closed
  • Created 2 years ago
  • Comments:14

github_iconTop GitHub Comments

1reaction
Ergamoncommented, Sep 13, 2021

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.

0reactions
rathoregeecommented, Sep 13, 2021

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 …

}

Read more comments on GitHub >

github_iconTop Results From Across the Web

Autofixture fixture setup questions #1291
So what happens is that AutoFixture tries to create an instance of a RequestChanged object and fails as this class has no public...
Read more >
Newest 'autofixture' Questions
I am writing unit test in mvc project with AutoFixture and NSubstitude. But I am getting object reference error in my update and...
Read more >
AutoMock setup with AutoFixture Data Theories
We can't do a fixture. Is there a way to trigger a setup method after AutoFixture generates an item but before it is...
Read more >
Why I stopped worrying about test setups by using ...
The answer to that question doesn't have to be complicated. In fact, AutoFixture is designed so that 80% of created specimens don't need...
Read more >
UnitTest With AutoFixture In .NET 6.0
In this tutorial, we are going to see how AutoFixture can be used to create robust test cases. Please refer the link to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found