Coder Social home page Coder Social logo

nightangell / signalr_unittestingsupport Goto Github PK

View Code? Open in Web Editor NEW
26.0 2.0 1.0 208 KB

Easy to use, small, SignalR Core unit testing support with NUnit, xUnit, MSTest and Moq. It`s also possibility to use it with custom testing engine. (Docs ready to use)

License: MIT License

C# 100.00%
signalr signalr-core signalrcor unit-testing nunit xunit mstest moq entity-framework-core

signalr_unittestingsupport's Introduction

For the latest versions of NUnit version see: https://www.nuget.org/packages/SignalR.UnitTestingSupport.NUnit/

For the latest versions of xUnit version see: https://www.nuget.org/packages/SignalR.UnitTestingSupport.xUnit

For the latest versions of MSTest version see: https://www.nuget.org/packages/SignalR.UnitTestingSupport.MSTest/

For full docs see: https://github.com/NightAngell/SignalR_UnitTestingSupport/wiki

What it is?

This lib provide support objects and base classes for testing Hub, Hub<T>, IHubContext<T>, IHubContext<T, P>. For example preconfigured mocks and helpfull verify methods. There is also support for testing Hubs and IHubContexts with Entity Framework Core.

Which nuget version should I choose?

  1. netcoreapp (2.1, 2,2, 3.0, 3,1) - 2.0.0
  2. net5 - 5.0.0
  3. net6.0 - 6.0.0
  4. net7.0 - 7.0.0
  5. net8.0 - 8.0.0

How to install

1. Using Visual Studio Nuget Package Manager

If you dont know how install nuget via Visual Studio see this: https://www.youtube.com/watch?v=jX5HlrerIos

SignalR.UnitTestingSupport.NUnit
SignalR.UnitTestingSupport.xUnit
SignalR.UnitTestingSupport.MSTest

2. Using Packet Manager Console

Install-Package SignalR.UnitTestingSupport.NUnit
Install-Package SignalR.UnitTestingSupport.xUnit
Install-Package SignalR.UnitTestingSupport.MSTest

3. Using .Net CLI

dotnet add package SignalR.UnitTestingSupport.NUnit
dotnet add package SignalR.UnitTestingSupport.xUnit
dotnet add package SignalR.UnitTestingSupport.MSTest

Troubleshooting!

Install NUnit3TestAdapter nuget for visual studio testing with NUnit in visual studio. https://www.nuget.org/packages/NUnit3TestAdapter/

Install xunit.runner.visualstudio nuget for visual studio testing with xUnit. https://www.nuget.org/packages/xunit.runner.visualstudio/

Install MSTest.TestAdapter nuget for visual studio testing with MSTest.
https://www.nuget.org/packages/MSTest.TestAdapter/

If your tests are not even detected, install Microsoft.Net.Test.Sdk nuget :)
https://www.nuget.org/packages/Microsoft.NET.Test.Sdk/

If you get NullReferenceException when you try testing with my lib see this.

Error after update for ASP.NET Core 3.0: Help with migration from ASP.NET Core 2.1 or 2.2 to 3.0

Help with migration from netstandard2.0 to netstandard.2.1 compatibile project

How to use

After you add nuget to your test project, SignalR_UnitTestingSupport is ready to use.

All IHubContext support classes are in SignalR_UnitTestingSupportCommon.IHubContextSupport namespace

For NUnit: All testing base classes are in SignalR_UnitTestingSupport.Hubs namespace

For xUnit: All testing base classes are in SignalR_UnitTestingSupportXUnit.Hubs namespace

For MSTest: All testing base classes are in SignalR_UnitTestingSupportMSTest.Hubs namespace

1) Base classes approach (Recommended)

Create test class for hub for example:

class ExampleHubTests {}

And then:

1. For testing Hub

class ExampleHubTests : HubUnitTestsBase {}

2. For testing Hub<T>

class ExampleHubTests : HubUnitTestsBase<T> {}

3. For testing Hub with EntityFrameworkCore

class ExampleHubTests : HubUnitTestsWithEF<TDbContext> {}

TDbContext is any class which inherit from Microsoft.EntityFrameworkCore.DbContext or DbContext itself.

4. For testing Hub<T> with EntityFrameworkCore

class ExampleHubTests : HubUnitTestsWithEF<T, TDbContext> {}

TDbContext is any class which inherit from Microsoft.EntityFrameworkCore.DbContext or DbContext itself.

5. For testing with IHubContext<THub> see this.

5. For testing with IHubContext<THub, TIHubResponses> see this.

2) Testing objects approach (Alternative, if you can't use base classes).

How use second approach. See this.

For full docs see: Docs

signalr_unittestingsupport's People

Contributors

nightangell avatar weihanli avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

weihanli

signalr_unittestingsupport's Issues

How to verify custom objects on SendCoreAsync rather than indivual arguments

I'm using this library to assist in unit testing a SignalR Hub with NUnit and as per best-practice, implementing custom objects, rather than individual parameters/arguments for SendAsync Hub methods to ensure backwards compatability.

I haven't been able to get the .Verify to pass on an example Hub method for some reason via SendCoreAsync when trying to assert on a custom object. Although, it does pass for a simplier example utilising a single argument/parameter. See below:

namespace MyExampleHub
{
    public class GameHub : Hub
    {
        public override async Task OnConnectedAsync()
        {
            await base.OnConnectedAsync();
        }

        public async Task AddUserTest(string name)
        {
            User user;
            user = new User(Context.ConnectionId, name);
            await Clients.Client(Context.ConnectionId).SendAsync("UpdateUser", user.Name);
        }
    }
}
namespace MyUserModel
{
    public class User
    {
        public User(string connectionId, string name)
        {
            ConnectionId = connectionId;
            Name = name;
        }

        public string ConnectionId { get; set; }
        public string Name { get; set; }

    }
}
namespace MyHubUnitTests
{
    public class GameHubTests : HubUnitTestsBase
    {
        [Test]
        public async Task AddUser_Should_Call_ClientsClient_with_username()
        {
            // Arrange
            var mockHub = new GameHub();
            AssignToHubRequiredProperties(mockHub);
            var username = "testuser";
            var connId = Guid.NewGuid().ToString();
            ContextMock.Setup(x => x.ConnectionId).Returns(connId);
            var mockUser = new User(connId, username);

            // Act
            await mockHub.AddUserTest(username);

            // Assert
            ClientsClientMock.Verify(c =>
                c.SendCoreAsync(
                    "UpdateUser",
                    new object[] { username },
                    It.IsAny<CancellationToken>())
                );

        }
    }
}

The above example test passes if I pass user.Name to SendAsync but not if I pass the user object and try to assert on:

ClientsClientMock.Verify(c =>
                c.SendCoreAsync(
                    "UpdateUser",
                    new object[] { mockUser },
                    It.IsAny<CancellationToken>())
                );

See output below from Moq:

Message:โ€‰
Moq.MockException : 
Expected invocation on the mock at least once, but was never performed: c => c.SendCoreAsync("UpdateUser", [User], It.IsAny<CancellationToken>())

Performed invocations:

   Mock<IClientProxy:4> (c):

      IClientProxy.SendCoreAsync("UpdateUser", [User], CancellationToken)

Appreciate any advice you can give on how I can get this to work :)

.net 5 Nugets

TODO: prepare and publish new nugets based on master

.NET 6 update

  • Check if it still works on .NET 6
  • Update dependencies
  • Fix bugs
  • Prepare new nugets
  • Release new nugets

.NET 8 update

  • Check if it still works on .NET 8
  • Fix bugs
  • Update dependencies
  • Prepare new nugets
  • Release new nugets

get the context.connectionid

What do you want to know?
Hello i am writing tests using this library.

I would like to know which connection id the library generates.
or How do i get the connectionId while testing?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.