Skip to content

Mock Type With Constructor Attributed With [SetsRequiredMembers] Must Have Attribute on Generated Mock Type #368

@JasonBock

Description

@JasonBock

To Reproduce
Create a test with this code:

var code =
  """
  using Rocks.Runtime;
  using System.Diagnostics.CodeAnalysis;

  [assembly: Rock(typeof(RequiredConstructor), BuildType.Create | BuildType.Make)]

  #nullable enable

  public class RequiredConstructor
  {
    [SetsRequiredMembers]
    public RequiredConstructor() { }

    public virtual object GetValue() => new();
  }
  """;

Expected behavior
Mock code is generated with no issues.

Actual behavior
Errors occur:

error CS9039: This constructor must add 'SetsRequiredMembers' because it chains to a constructor that has that attribute.

Additional context
Note that the RequiredConstructor type doesn't actually have any required properties, but it's OK to put this attribute on the constructor in this case. If it had required properties, Rocks will automatically put [SetsRequiredMembers] on the constructor. However, it also needs to do it if it exists on the base type's constructor as well.

This was found on Elastic.Clients.Elasticsearch.Sql.QueryResponse, version 9.0.1

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions