Static member error with instance member, xUnit
Asked Answered
G

0

2

"An object reference is required for the non-static field" I have a static member which cannot be an instance member. This static member uses an instance of an object that is initialized in the constructor of the class through dependency injection, so it cannot be static. How could I solve this error?

public class My
{
    private readonly Fixture _fixture;

    public MyClass(Fixture fixture)
    {
        _fixture = fixture;
    }

    public static IEnumerable<object[]> TestCases()
    {
        yield return new object[] { Urls.BuildUrl(_fixture.WebAppUrl)};

    }

    [Theory]
    [MemberData(nameof(TestCases))]
    ...

public static class Urls
{
    public static string BuildUrl(string url)
    {
    ...

The error is thrown here:

yield return new object[] { Urls.**BuildUrl**(_fixture.WebAppUrl)};
Goaltender answered 9/3, 2020 at 17:13 Comment(4)
The question in its current state is incomplete and therefore unclear. It would be awesome if you could share a minimal reproducible example so we get a clearer picture of the current problem and what you are actually trying to do?Closegrained
Added more context.Goaltender
Does Urls actually need to be a static class, or can it be non-static with static methods on it?Hemorrhoidectomy
I shouldn't change Url, but I tried just to check out, turning Url into non-static doesn't fix the error.Goaltender

© 2022 - 2024 — McMap. All rights reserved.