我在尝试使用Moq和RestSharp时遇到了一些挑战.也许这是我对Moq的误解,但由于某种原因,我在尝试模拟RestResponse时继续获得空引用异常.
这是我的单元测试.
[Test]
public void GetAll_Method_Throws_exception_if_response_Data_is_Null()
{
var restClient = new Mock<IRestClient>();
restClient.Setup(x => x.Execute(It.IsAny<IRestRequest>()))
.Returns(new RestResponse<RootObjectList>
{
StatusCode = HttpStatusCode.OK,
Content = null
} );
var client = new IncidentRestClient(restClient.Object);
Assert.Throws<Exception>(() => client.GetAll());
}
Run Code Online (Sandbox Code Playgroud)
这是我的实际实现:
public class IncidentRestClient : IIncidentRestClient
{
private readonly IRestClient client;
private readonly string url = "some url here";
public IncidentRestClient()
{
client = new RestClient { BaseUrl = new Uri(url) };
}
public RootObjectList GetAll()
{
var request = new RestRequest("api/now/table/incident", Method.GET) { RequestFormat …Run Code Online (Sandbox Code Playgroud) 我正在努力弄清楚如何根据 Dictionary 对象中的键映射值。
这是我到目前为止所拥有的:
.ForMember(dest => dest.Submitter,
opts => opts.MapFrom(src => src.opened_by))
Run Code Online (Sandbox Code Playgroud)
注意:src.open_by 是一个字典对象。我想按键搜索以获取映射到 dest.Submitter 的值
附加信息:
这是目标对象:
public class Incident
{
public int Active { get; set; }
public string Submitter { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是源对象:
Dictionary<string, string> opened_by = new Dictionary<string, string>
{
{ "link", "https://someurl/674bd1f96f03e10071c35e02be3ee4ae" },
{ "value", "674bd1f96f03e10071c35e02be3ee4ae" }
};
Run Code Online (Sandbox Code Playgroud)
我的目标是从“value”键获取值 674bd1f96f03e10071c35e02be3ee4ae 以水合事件对象的“Submitter”属性
谢谢!:)