Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it possible to create unit tests for Actors #173

Closed
Xorpio opened this issue Nov 26, 2019 · 9 comments · Fixed by #220
Closed

Make it possible to create unit tests for Actors #173

Xorpio opened this issue Nov 26, 2019 · 9 comments · Fixed by #220

Comments

@Xorpio
Copy link

Xorpio commented Nov 26, 2019

Right now i can't create a actor inside a unit test. Take the following Actor code for example

public class TestActor : Actor
{
    public TestActor(ActorService actorService, ActorId actorId) : base(actorService, actorId)
    {
    }

    public async Task<string> GetString()
    {
        return await StateManager.GetStateAsync<string>("Test");
    }
}

And using the following unit test:

    [Test]
    public async Task CanCreateActor()
    {
        var actor = new TestActor(
            new ActorService(
                new ActorTypeInformation()
            ),
            new ActorId("Test")
        );

        Assert.IsInstanceOf<Actor>(actor);
    }

When i run the unit test it throws a NullRefference exception

Message: 
    System.NullReferenceException : Object reference not set to an instance of an object.
  Stack Trace: 
    Actor.ctor(ActorService actorService, ActorId actorId)
    TestActor.ctor(ActorService actorService, ActorId actorId) line 12
    IAMEmployeeTest.CanCreateActor() line 19
    GenericAdapter`1.GetResult()
    AsyncToSyncAdapter.Await(Func`1 invoke)
    TestMethodCommand.RunTestMethod(TestExecutionContext context)
    TestMethodCommand.Execute(TestExecutionContext context)
    SimpleWorkItem.PerformWork()
@amanbha
Copy link
Contributor

amanbha commented Dec 5, 2019

@Xorpio Thanks for reporting. Will using Moq and setting them correctly for ActorService help in this case? Or do you have other suggestions in mind?

@Xorpio
Copy link
Author

Xorpio commented Dec 11, 2019

The problem is that there are some hardcoded dependencies that cannot be faked. (as far as i know). Like on this line:
https://github.com/dapr/dotnet-sdk/blob/master/src/Dapr.Actors/Runtime/Actor.cs#L42

@malotho-zz
Copy link

@Xorpio please check out this work.
https://github.com/Ignition-Group-Open-Source-Contrib/DaprDotnetActorUnitests. Code coverage, and 100% testebility on the actor.

@Xorpio
Copy link
Author

Xorpio commented Jan 31, 2020

Oh nice work. it looks like a working solution. I can look further in depth in the coming days.

@malotho-zz
Copy link

@amanbha should this issue be closed?

@amanbha
Copy link
Contributor

amanbha commented Feb 12, 2020

@malotho I would suggest to keep it open to track more improvements needed in SDK itself to make it easier to write unit tests.

@malotho-zz
Copy link

@amanbha that makes sense.

@amanbha
Copy link
Contributor

amanbha commented Feb 14, 2020

Keeping this open for tracking more improvements to making actor unit tests

@onionhammer
Copy link
Contributor

The advice here appears very outdated... is there any examples with updated SDK references on how to do unit testing with actors?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants