shivam51
shivam51
CC#
Created by shivam51 on 3/14/2023 in #help
❔ Not able to mock a daprclient function in dotnet
I am unit testing a function where I use the InvokeMethodAsync method of DaprClient but I constantly get the error:
error NS1000: Member InvokeMethodAsync can not be intercepted. Only interface members and virtual, overriding, and abstract members can be intercepted.
error NS1000: Member InvokeMethodAsync can not be intercepted. Only interface members and virtual, overriding, and abstract members can be intercepted.
. for the code
_daprClient
.InvokeMethodAsync<TransactionResponse>(HttpMethod.Get, "svc-transactions", "123")
.Returns(transactionResponse);
_daprClient
.InvokeMethodAsync<TransactionResponse>(HttpMethod.Get, "svc-transactions", "123")
.Returns(transactionResponse);
How to fix this? I am using xUnit along with nSubstitute for mocking.
13 replies
CC#
Created by shivam51 on 3/12/2023 in #help
❔ How can I mock a Service class being used in Component class?
The service also initializes a dapr component in it's constructor. I want to unit test a function of the controller class. Happy to share more info.
6 replies
CC#
Created by shivam51 on 1/4/2023 in #help
❔ How to install a new .net project from github
I want to share my .net project in github what should be the installation steps for it? I can see all the dependencies in the .csproj file but how to install all of them in one go?
24 replies
CC#
Created by shivam51 on 1/2/2023 in #help
❔ What is the use of datatype after function name?
I have a function signature that looks like this:
c#
public static IServiceCollection AddMongoRepository<T>(this IServiceCollection services, string collectionName) where T : IEntity
c#
public static IServiceCollection AddMongoRepository<T>(this IServiceCollection services, string collectionName) where T : IEntity
What is the use of <T> after the function name?
18 replies
CC#
Created by shivam51 on 12/30/2022 in #help
❔ Registering a class with string parameter for DI.
This I have a class MongoRepository which accepts a MongoDB database and collection name in the constructor parameters. How do I fix this file?
c#

using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Serializers;
using MongoDB.Driver;
using Play.Catalog.Service.Entities;
using Play.Catalog.Service.Settings;

namespace Play.Catalog.Service.Repositories
{
public static class Extensions
{
public static IServiceCollection AddMongo(this IServiceCollection services)
{
BsonSerializer.RegisterSerializer(new GuidSerializer(BsonType.String));
BsonSerializer.RegisterSerializer(new DateTimeOffsetSerializer(BsonType.String));

services.AddSingleton(provider =>
{
var configuration = provider.GetRequiredService<IConfiguration>();
// This configuration compiles to be null.
var mongoDbSettings = configuration.GetSection(nameof(MongoDbSettings)).Get<MongoDbSettings>();
var mongoClient = new MongoClient(mongoDbSettings.ConnectionString);
return mongoClient.GetDatabase("service");
});
return services;
}
}
}
c#

using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Serializers;
using MongoDB.Driver;
using Play.Catalog.Service.Entities;
using Play.Catalog.Service.Settings;

namespace Play.Catalog.Service.Repositories
{
public static class Extensions
{
public static IServiceCollection AddMongo(this IServiceCollection services)
{
BsonSerializer.RegisterSerializer(new GuidSerializer(BsonType.String));
BsonSerializer.RegisterSerializer(new DateTimeOffsetSerializer(BsonType.String));

services.AddSingleton(provider =>
{
var configuration = provider.GetRequiredService<IConfiguration>();
// This configuration compiles to be null.
var mongoDbSettings = configuration.GetSection(nameof(MongoDbSettings)).Get<MongoDbSettings>();
var mongoClient = new MongoClient(mongoDbSettings.ConnectionString);
return mongoClient.GetDatabase("service");
});
return services;
}
}
}
I am using this file in the Program.cs as
builder.Services.AddMongo();
builder.Services.AddMongo();
i
15 replies