小编pla*_*ezy的帖子

如何使用 Moq 模拟 IConfiguration?

如何在单元测试中模拟这样的代码。我在 asp.net core 5 中使用 xUnit 和 Moq。我是 xUnit 和 Moq 的新手。

var url = configuration.GetSection("AppSettings").GetSection("SmsApi").Value;
Run Code Online (Sandbox Code Playgroud)

配置对象已经注入到构造函数中。

这是我目前在单元测试课程中的内容

public class UtilityTests
{
    private readonly Utility sut;

    public UtilityTests()
    {
        var mockConfig = new Mock<IConfiguration>();
        var mockConfigSection = new Mock<IConfigurationSection>();
        //mockConfigSection.Setup(x => x.Path).Returns("AppSettings");
        mockConfigSection.Setup(x => x.Key).Returns("SmsApi");
        mockConfigSection.Setup(x => x.Value).Returns("http://example.com");
        
        mockConfig.Setup(x => x.GetSection("AppSettings")).Returns(mockConfigSection.Object);
        
        sut = new Utility(mockConfig.Object);
    }

    [Fact]
    public void SendSmsShdReturnTrue()
    {
        var fixture = new Fixture();
        
        var result = sut.SendSms(fixture.Create<string>(), fixture.Create<string>());
        result.Should().BeTrue();
    }
}
Run Code Online (Sandbox Code Playgroud)

c# configuration moq xunit asp.net-core-mvc

7
推荐指数
2
解决办法
9388
查看次数

如何使用LinqToExcel获取excel文件的工作表名称

我正在使用LinqToExcel,我希望能够获取excel文件中所有工作表的名称,并将其与我的html表单中的输入进行比较,这样当输入与excel表上的任何名称都不匹配时,系统将抛出一个例外.我如何使用LinqToExcel来执行此操作

c# asp.net-mvc linq-to-excel

4
推荐指数
1
解决办法
6785
查看次数

如何在C#中反序列化JSON?

有人可以用这种格式帮助我以正确的方式反序列化json:

[
    {person: {name: "James", age:26}},
    {person: {name: "Mary", age:36}},
    {person: {name: "Kofi", age:46}}
]
Run Code Online (Sandbox Code Playgroud)

我正在使用的代码如下:

WebRequest request = WebRequest.Create("url");
WebResponse response = request.GetResponse();

string json;

using (var sr = new StreamReader(response.GetResponseStream()))
{
    json = sr.ReadToEnd();
}

var serializer = new JavaScriptSerializer();

var persons= serializer.Deserialize<List<response>>(json);

foreach (var item in persons)
{
    Console.Write("name:" + item.name + " and age: " + item.age);
}
Run Code Online (Sandbox Code Playgroud)

我正在映射的类如下:

public class person
{
    public string name{get;set;}
    public int age{get; set;}
}

public class response
{
   public …
Run Code Online (Sandbox Code Playgroud)

json deserialization c#-4.0

1
推荐指数
1
解决办法
7087
查看次数

上传文件时System.UnauthorizedAccessException

每当我尝试在ASP.NET MVC应用程序中上传文件时,我都会收到此错误.谁能帮我吗.抛出的错误是System.UnauthorizedAccessException

访问路径'C:\ inetpub\wwwroot\IBGTxalert\TxAlert\TxAlert.Web\Files\linqtoexcel.xlsx'被拒绝.

c# model-view-controller asp.net-mvc

0
推荐指数
1
解决办法
3900
查看次数