参数1:无法在Xunit测试类中从“对象”转换为“字符串”

时间:2018-12-06 12:57:03

标签: asp.net-core .net-core asp.net-core-mvc xunit

我在Xunit测试类中遇到以下错误。

Argument 1: cannot convert from 'object' to 'string'.

代码通过以下方式返回数据

    [HttpPost]
    [Route("Reg")]
    public async Task <dynamic> User([FromBody] object value)
    {
        //User Registeration
       // To Get All Data
        JObject ReturnData = new JObject();
        try
        {
            var Collection = _database.GetCollection<BsonDocument>("MyCollectio");
            var doc = await Collection.Find(new BsonDocument()).ToListAsync();

            if(doc != null)
            {

                    // Data.Add("userid",JsonConvert.SerializeObject(doc));
                    ReturnData.Add("success",1);
                    ReturnData.Add("message","Data Successfully Inserted");
                    ReturnData.Add("data",doc.ToJson());
            }
            else
            {
                ReturnData.Add("success",0);
                ReturnData.Add("message","No Data Found");
            }
        }
        catch(Exception e)
        {
            Console.WriteLine(e.StackTrace);
            ReturnData.Add("success",0);
            ReturnData.Add("message","Error");
        }
        return ReturnData;
    }

测试类

    [Fact]
    public async void Test1()
    {
        JObject jObject = new JObject();
        jObject.Add("name", "Satheesh");
        jObject.Add("email", "sat@sat.com");

        JObject finaljObject = new JObject();
        finaljObject.Add("data", finaljObject);

        var content = JsonConvert.SerializeObject(finaljObject);
        var stringContent = new StringContent(content, Encoding.UTF8, "application/json");

        // Act
        var response = await _client.PostAsync("/api/Values/Reg", stringContent);

        var t =  response;

        // Assert
        // response.StatusCode.ShouldBe(HttpStatusCode.OK);
        var result = await response.Content.ReadAsStringAsync();
        var responseString = await response.Content.ReadAsStringAsync();
        Assert.Contains("Data Successfully Retrived", responseString);
    }

实际代码正常运行,没有任何错误。当我尝试运行测试调用时,它会在我的实际项目中显示错误。我该如何解决。

谢谢...

0 个答案:

没有答案