Is this controller untestable?









up vote
0
down vote

favorite












In its present state I dont think I can test it because of the dependencies on user authentication (user.identity) and route data (RouteData.Values), original plan was to check if it was returning a view. Should I try to mock/fake these values or not be testing a controller like this in first place?






public IActionResult Index()












share|improve this question

















  • 1




    You question is basically "should I test this code". The answer is pretty much opinion based so I'm voting to close it as such.
    – Esko
    yesterday










  • Are there rules/suggestions around which parts of the code are suitable for testing and which type, ie unit,integration, system?
    – CerIs
    yesterday














up vote
0
down vote

favorite












In its present state I dont think I can test it because of the dependencies on user authentication (user.identity) and route data (RouteData.Values), original plan was to check if it was returning a view. Should I try to mock/fake these values or not be testing a controller like this in first place?






public IActionResult Index()












share|improve this question

















  • 1




    You question is basically "should I test this code". The answer is pretty much opinion based so I'm voting to close it as such.
    – Esko
    yesterday










  • Are there rules/suggestions around which parts of the code are suitable for testing and which type, ie unit,integration, system?
    – CerIs
    yesterday












up vote
0
down vote

favorite









up vote
0
down vote

favorite











In its present state I dont think I can test it because of the dependencies on user authentication (user.identity) and route data (RouteData.Values), original plan was to check if it was returning a view. Should I try to mock/fake these values or not be testing a controller like this in first place?






public IActionResult Index()












share|improve this question













In its present state I dont think I can test it because of the dependencies on user authentication (user.identity) and route data (RouteData.Values), original plan was to check if it was returning a view. Should I try to mock/fake these values or not be testing a controller like this in first place?






public IActionResult Index()








public IActionResult Index()





public IActionResult Index()






.net unit-testing






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked yesterday









CerIs

13418




13418







  • 1




    You question is basically "should I test this code". The answer is pretty much opinion based so I'm voting to close it as such.
    – Esko
    yesterday










  • Are there rules/suggestions around which parts of the code are suitable for testing and which type, ie unit,integration, system?
    – CerIs
    yesterday












  • 1




    You question is basically "should I test this code". The answer is pretty much opinion based so I'm voting to close it as such.
    – Esko
    yesterday










  • Are there rules/suggestions around which parts of the code are suitable for testing and which type, ie unit,integration, system?
    – CerIs
    yesterday







1




1




You question is basically "should I test this code". The answer is pretty much opinion based so I'm voting to close it as such.
– Esko
yesterday




You question is basically "should I test this code". The answer is pretty much opinion based so I'm voting to close it as such.
– Esko
yesterday












Are there rules/suggestions around which parts of the code are suitable for testing and which type, ie unit,integration, system?
– CerIs
yesterday




Are there rules/suggestions around which parts of the code are suitable for testing and which type, ie unit,integration, system?
– CerIs
yesterday












1 Answer
1






active

oldest

votes

















up vote
1
down vote













You can inject IHttpContextAccessor into your controller and use _httpContext.GetRouteData() and _httpContext.User to make it testable.



Read this article to understand what to test and how to write testable code - https://www.toptal.com/qa/how-to-write-testable-code-and-why-it-matters






share|improve this answer




















  • Seems little bid odd injecting IHttpContextAccessor, because controller already have access to the current HttpContext
    – Fabio
    yesterday










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53224410%2fis-this-controller-untestable%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote













You can inject IHttpContextAccessor into your controller and use _httpContext.GetRouteData() and _httpContext.User to make it testable.



Read this article to understand what to test and how to write testable code - https://www.toptal.com/qa/how-to-write-testable-code-and-why-it-matters






share|improve this answer




















  • Seems little bid odd injecting IHttpContextAccessor, because controller already have access to the current HttpContext
    – Fabio
    yesterday














up vote
1
down vote













You can inject IHttpContextAccessor into your controller and use _httpContext.GetRouteData() and _httpContext.User to make it testable.



Read this article to understand what to test and how to write testable code - https://www.toptal.com/qa/how-to-write-testable-code-and-why-it-matters






share|improve this answer




















  • Seems little bid odd injecting IHttpContextAccessor, because controller already have access to the current HttpContext
    – Fabio
    yesterday












up vote
1
down vote










up vote
1
down vote









You can inject IHttpContextAccessor into your controller and use _httpContext.GetRouteData() and _httpContext.User to make it testable.



Read this article to understand what to test and how to write testable code - https://www.toptal.com/qa/how-to-write-testable-code-and-why-it-matters






share|improve this answer












You can inject IHttpContextAccessor into your controller and use _httpContext.GetRouteData() and _httpContext.User to make it testable.



Read this article to understand what to test and how to write testable code - https://www.toptal.com/qa/how-to-write-testable-code-and-why-it-matters







share|improve this answer












share|improve this answer



share|improve this answer










answered yesterday









Sergey Kolodiy

4,26312344




4,26312344











  • Seems little bid odd injecting IHttpContextAccessor, because controller already have access to the current HttpContext
    – Fabio
    yesterday
















  • Seems little bid odd injecting IHttpContextAccessor, because controller already have access to the current HttpContext
    – Fabio
    yesterday















Seems little bid odd injecting IHttpContextAccessor, because controller already have access to the current HttpContext
– Fabio
yesterday




Seems little bid odd injecting IHttpContextAccessor, because controller already have access to the current HttpContext
– Fabio
yesterday

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53224410%2fis-this-controller-untestable%23new-answer', 'question_page');

);

Post as a guest














































































Popular posts from this blog

How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

Syphilis

Darth Vader #20