小编Pio*_*r X的帖子

NSubstitute:模拟属性

我在单元测试中创建了替代品Person和类。AddressBook该类包含类型和名称AddressBook的属性: 。PersonSamplePerson

public interface IAddressBook
{
    Person SamplePerson { get; set; }
}

public class AddressBook : IAddressBook
{
    public Person SamplePerson { get; set; }

    public AddressBook(Person samplePerson)
    {
        SamplePerson = samplePerson;
    }
}

public interface IPerson
{
    string GetName(string name);
}

public class Person : IPerson
{
    public string GetName(string name)
    {
        return name;
    }
}

public void TestMethod1()
{
    var personMock = Substitute.For<IPerson>();
    var addressBookMock = Substitute.For<IAddressBook>();

    addressBookMock.SamplePerson.Returns(personMock); //not …
Run Code Online (Sandbox Code Playgroud)

c# unit-testing mocking nsubstitute

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

使用 Task.WhenAll 时线程安全

public async Task<Informations> DoSthAsync
{
   var informations = new List<Informations>();
   await Task.WhenAll(FirstTask(informations), SecondTask(informations));

   return informations;
}

public async Task FirstTask(List<Informations> list)
{
   await Task.Run( () => //do sth with list);
}

public async Task SecondTask(List<Informations> list)
{ 
  await Task.Run( () => //do sth with list);
}
Run Code Online (Sandbox Code Playgroud)

我想问一下,当两个任务使用同一个列表时,这段代码是否会导致问题?

谢谢。

c# multithreading thread-safety

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