I have a Web api with a function like this:
[HttpPost]
public IHttpActionResult PostData([FromBody] JObject new_data)
{
//Some code here
var fullPath = System.Web.Hosting.HostingEnvironment.MapPath(@"~/App_Data/data.txt");
System.IO.File.WriteAllText(fullPath, new_data.ToString());
return Ok();
}
Then I am testing this api like this:
[TestMethod]
public void TestPostData()
{
// Arrange
JObject data = getSampleData();
var controller = new MyController();
//Act
IHttpActionResult result = controller.PostData(data);
//Assert
Assert.IsInstanceOfType(result, typeof(OkNegotiatedContentResult<JObject>));
}
Here I get error at the line below:
System.IO.File.WriteAllText(fullPath, new_data.ToString());
And the error says: Message: Test method NetWorth.Tests.UnitTest1.TestPostData threw exception: System.ArgumentNullException: Value cannot be null. Parameter name: path
Can someone help please? I'm very new to ASP and c# and unit testing! Any suggestions would help a lot.
I should say that the api works perfect when I run it and this problem is just at the time of testing.
for the test, use the full path to the file instead of var fullPath = System.Web.Hosting.HostingEnvironment.MapPath(@"~/App_Data/data.txt");
for example, let the method take a file path that, if not passed, will be equal to your constant
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.