c# - RestSharp Unit Test NUnit Moq RestResponse null reference exception -
i'm having challenges trying use moq restsharp. maybe it's misunderstanding of moq reason keep on getting null reference exception when trying mock restresponse.
here unit test.
[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()); } here implementation:
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 = dataformat.json }; request.onbeforedeserialization = resp => { resp.contenttype = "application/json"; }; irestresponse<rootobjectlist> response = client.execute<rootobjectlist>(request); if (response.data == null) throw new exception(response.errorexception.tostring()); return response.data; } } for reason response object null. i'm mocking return object incorrectly?
for disclosure purposes, assuming incidentrestclient has constructor takes irestclient instance parameter , uses set client member.
it looks like, in test, running setup different overload of execute 1 using. instead of:
.setup(x => x.execute( try:
.setup(x => x.execute<rootobjectlist>(
Comments
Post a Comment