Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

211
Visualizações
Change behaviour of AutoFixture with AutoMoq to return false for methods

Say that I have the following interface:

public interface ITeam
{
    bool HasPlayer(IPlayer player);
    void AddPlayer(IPlayer player);
}

I currently have a test that looks something along the lines of (using AutoMoq):

[Theory]
[MyAutoData]
public void ShouldRosterToTeamWhenPlayerIsNotRostered(Player player, Mock<ITeam> mockedTeam)
{
    player.RosterToTeam(mockedTeam.Object);

    mockedTeam.Verify(team => team.AddPlayer(player), Times.Once);
}

However, a precondition in my code is that HasPlayer must return false for the test to pass when RosterToTeam is called.

This can be solved by creating a ICustomization and directly composing the correct behaviour, for example:

public class TeamCustomization : ICustomization
{
    public void Customize(IFixture fixture)
    {
        fixture.Customize<Mock<ITeam>>(composer =>
            composer.Do(mock =>
                        mock.Setup(team => team.HasPlayer(It.IsAny<IPlayer>()))
                            .Returns(false)));
    }
}

However, I'd like my tests always to assume that the boolean methods have a default value of false. I've tried looking into ISpecimenBuilder, but couldn't see a way to achieve this, as it seems to only work on properties, parameters, etc.

Is anyone able to recommend me a way of generically setting up all boolean methods to return false by default when created in this fashion?

Edit: The culprit behind the behaviour change is when ConfigureMembers = true is set for AutoMoqCustomization.

This is what my MyAutoDataAttribute currently looks like:

public class MyAutoDataAttribute : AutoDataAttribute
{
    public MyAutoDataAttribute() : base(Create)
    {
    }

    private static IFixture Create()
    {
        var fixture = new Fixture();

        fixture.Customize(new AutoMoqCustomization
        {
            ConfigureMembers = true
        });

        fixture.Customize(new TeamCustomization());

        return fixture;
    }
}

For my use case, ConfigureMembers = true is still needed (and would like to remove the TeamCustomization).

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

First of all, you may want to use AutoMoqDataAttribute to create a mock of the ITeam interface:

public class AutoMoqDataAttribute : AutoDataAttribute
{
    public AutoMoqDataAttribute()
        : base(new Fixture().Customize(new AutoMoqCustomization()))
    {
    }
}

There is no need in cusomizing fixture to configure your mocks. You'd better do that in the tests itself (the arrange section):

[Theory, AutoMoqData]
public void ShouldRosterToTeamWhenPlayerIsNotRostered(Player player, Mock<ITeam> mockedTeam)
{
    mockedTeam.Setup(t => t.HasPlayer(player)).Returns(false);
    player.RosterToTeam(mockedTeam.Object);
    mockedTeam.Verify(team => team.AddPlayer(player), Times.Once);
}

[Theory, AutoMoqData]
public void ShouldNotRosterToTeamWhenPlayerIsRostered(Player player, Mock<ITeam> mockedTeam)
{
    mockedTeam.Setup(t => t.HasPlayer(player)).Returns(true);
    player.RosterToTeam(mockedTeam.Object);
    mockedTeam.Verify(team => team.AddPlayer(player), Times.Never);
}

and, finaly, the simplified RoastToTeam implementation:

public class Player
{
    public void RosterToTeam(ITeam team)
    {
        if (team.HasPlayer(this))
        {
            return;
        }
        team.AddPlayer(this);
    }
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda