GithubHelp home page GithubHelp logo

programsolutions / integrationtestforsignalr Goto Github PK

View Code? Open in Web Editor NEW

This project forked from bekallaev/iisandintegrationtestsforsignalr

0.0 0.0 0.0 210 KB

Tutorial on how to write integration test for SignalR hub. Client is Blazor

JavaScript 10.24% C# 27.42% CSS 25.97% HTML 36.37%

integrationtestforsignalr's Introduction

IntegrationTestForSignalR

Tutorial on how to write integration test for SignalR hub. Client is Blazor

Instructions

  1. Complete this tutorial - https://learn.microsoft.com/en-us/aspnet/core/blazor/tutorials/signalr-blazor?view=aspnetcore-7.0&tabs=visual-studio&pivots=webassembly

Before going to next step I recommend you place literals related to signalr to constants in my case
I created SignalRConstants class with two constants. First one is SendMessageHandlerName and second one is
ChatHubRouteName

  1. Add test project. In my case I added xUnit test project
  2. Add link to your server project. In my case I add link to the BlazorSignalR.Server
  3. Add this list of NuGet packages:
  • Moq
  • FluentAssertion
  • Microsoft.AspNetCore.Mvc.Testing
  • Microsoft.AspNetCore.SignalR.Client
  1. Add test class with name - ChatHubTest

IMPORTANT NOTE: All next code changes will take place in one class, in ChatHubTest class

  1. Derive class from IClassFixture<WebApplicationFactory<Program>> so your class will look like this:
public class ChatHubTests : IClassFixture<WebApplicationFactory<Program>>
{}

Program class is the entry point of my BlazorSignalR.Server project

By implementing IClassFixture<T> interface you create one context of T for whole tests in the current class
more you can read here - https://xunit.net/docs/shared-context#class-fixture

WebApplicationFactory<TProgram> is the class that create the system that will be under the test, where
TProgram is the class that contains the startup logic of your application. There is a big reason why you should you use it, please read this article about why you should consider to use this class if you do integration test on .NET 7

  1. Add constructor to your class. Constructor should accept WebApplicationFactory<Program> factory so your code will be like this:
public class ChatHubTests : IClassFixture<WebApplicationFactory<Program>>
{
    private readonly WebApplicationFactory<Program> _factory;

    public ChatHubTests(WebApplicationFactory<Program> factory)
    {
        _factory = factory;
    }
}
  1. Add StartConnectionAsync method that create connection with SignlaR hub - ChatHub. Result:
private async Task<HubConnection> StartConnectionAsync(HttpMessageHandler handler, string hubName)
{
    var hubConnection = new HubConnectionBuilder()
        .WithUrl($"ws://localhost/{hubName}", o =>
        {
            o.HttpMessageHandlerFactory = _ => handler;
        })
        .Build();

    await hubConnection.StartAsync();

    return hubConnection;
}

You can see that url contains "ws" instead of "https". This is important since signlar prefered way of communication is web socket

  1. Add the test:
[Fact]
public async Task ShouldNotifySubscribers()
{
    _factory.CreateClient(); // You need to call this procedure in order to create server
    var mockHandler = new Mock<Action<string, string>>(); 

    // Arrange

    var connection = await StartConnectionAsync(_factory.Server.CreateHandler(), SignalRConstants.ChatHubRouteName);
    //Register handler with some name, message will be sent from server to the handler with this name
    connection.On(SignalRConstants.SendMessageHandlerName, mockHandler.Object);

    var sourceUserName = "super_user";
    var sourceMessage = "Hello World!!";

    // Act
    // Send message to the ChatHub(to the server), to the SendMessage method(method with such name exist in ChatHub)
    await connection.InvokeAsync(nameof(ChatHub.SendMessage), sourceUserName, sourceMessage);

    // Assert
    // Make sure that handler was called once and that handler have recieved correct input values
    mockHandler.Verify(x => x(It.Is<string>(userName => userName == sourceUserName), It.Is<string>(message => message == sourceMessage)), Times.Once);
}

integrationtestforsignalr's People

Contributors

bekallaev avatar

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.