Facing errors during the unit test for sample .NET micro-service (vb)

42 views Asked by At

I have a sample micro-service written in visual basic in .NET framework it basically prints the message hello on localhost, Now I am trying to do unit test for it using NUnit. I want the test to check my Program.vb file and in that compare the string mentioned there with "Hello" is they match the test should pass but I am facing an error. Below is my Program.vb file;`

Imports System
Imports System.Net
Imports System.Threading.Tasks
Imports Microsoft.AspNetCore.Builder
Imports Microsoft.AspNetCore.Hosting
Imports Microsoft.AspNetCore.Http
Imports Microsoft.Extensions.DependencyInjection
Imports Microsoft.Extensions.Logging
Imports Microsoft.FeatureManagement

Namespace HelloService
Public Class Program
    Private Const HelloFeatureEnabled As Boolean = True

    Public Shared Sub Main(args As String())
        CreateWebHostBuilder(args).Build().Run()
    End Sub

    Public Shared Function CreateWebHostBuilder(args() As String) As IWebHostBuilder
        Return New WebHostBuilder().UseKestrel().ConfigureServices(Sub(services)
                                                                       
services.AddLogging()
                                                                       
services.AddFeatureManagement()
                                                                   End 
Sub).Configure(Sub(app)
                                                                                         
app.UseMiddleware(Of FeatureToggleMiddleware)(HelloFeatureEnabled)
                                                                                         
 app.Run(AddressOf HandleRequest)
                                                                                     
End Sub)
    End Function

    Public Shared Async Function HandleRequest(context As HttpContext) As Task
        context.Response.ContentType = "text/plain"
        Await context.Response.WriteAsync("Hello")
    End Function
 End Class

 Public Class FeatureToggleMiddleware
    Private ReadOnly _next As RequestDelegate
    Private ReadOnly _logger As ILogger(Of FeatureToggleMiddleware)
    Private ReadOnly _helloFeatureEnabled As Boolean

    Public Sub New(ByVal nextItem As RequestDelegate, ByVal logger As ILogger(Of 
FeatureToggleMiddleware), ByVal helloFeatureEnabled As Boolean)
        _next = nextItem
        _logger = logger
        _helloFeatureEnabled = helloFeatureEnabled
    End Sub

        Public Async Function InvokeAsync(ByVal context As HttpContext) As Task
              If _helloFeatureEnabled Then
                _logger.LogInformation("HelloFeature is enabled")
                Await _next.Invoke(context)
            Else
                _logger.LogInformation("HelloFeature is disabled")
                context.Response.StatusCode = StatusCodes.Status404NotFound
            End If
        End Function
    End Class
End Namespace

And below is mu UnitTest1.vb file;

Imports NUnit.Framework
Imports HelloService ' Adjusted namespace
Imports Microsoft.AspNetCore.Http

Namespace HelloService.Tests ' Adjusted namespace
<TestFixture>
 Public Class UnitTest1
<Test>
   Public Async Sub TestHandleRequest() 
   Dim context As New DefaultHttpContext()
   Dim expectedResponse As String = "Hello"
   Dim actualResponse As String = Await 
   HelloService.Program.HandleRequest(context)  
   Assert.That(expectedResponse, Is.EqualTo(actualResponse))
   End Sub
   End Class
End Namespace

I am facing this error;

" error BC30456: 'Program' is not a member of 'HelloService.Tests.HelloService'. 
     [/home/cl/Desktop/New Folder/sample- 
     services/HelloService.Tests/HelloService.Tests.vbproj] "

Can anyone please help me with this.

0

There are 0 answers