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()


BlogHomeVM vm = new BlogHomeVM();

int skip = int.TryParse((string)this.RouteData.Values["skip"], out skip) ? skip : 0;
int showPosts = 3;

vm.PageTitle = "Async and wait";

vm.totalPosts = _context.Posts.Where(y => y.PublishedFrom <= DateTime.Now).Count();

vm.Posts = _context.Posts
.Where(y => y.PublishedFrom <= DateTime.Now












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
    Nov 9 at 11:01










  • Are there rules/suggestions around which parts of the code are suitable for testing and which type, ie unit,integration, system?
    – CerIs
    Nov 9 at 11:02














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()


BlogHomeVM vm = new BlogHomeVM();

int skip = int.TryParse((string)this.RouteData.Values["skip"], out skip) ? skip : 0;
int showPosts = 3;

vm.PageTitle = "Async and wait";

vm.totalPosts = _context.Posts.Where(y => y.PublishedFrom <= DateTime.Now).Count();

vm.Posts = _context.Posts
.Where(y => y.PublishedFrom <= DateTime.Now












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
    Nov 9 at 11:01










  • Are there rules/suggestions around which parts of the code are suitable for testing and which type, ie unit,integration, system?
    – CerIs
    Nov 9 at 11:02












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()


BlogHomeVM vm = new BlogHomeVM();

int skip = int.TryParse((string)this.RouteData.Values["skip"], out skip) ? skip : 0;
int showPosts = 3;

vm.PageTitle = "Async and wait";

vm.totalPosts = _context.Posts.Where(y => y.PublishedFrom <= DateTime.Now).Count();

vm.Posts = _context.Posts
.Where(y => y.PublishedFrom <= DateTime.Now












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()


BlogHomeVM vm = new BlogHomeVM();

int skip = int.TryParse((string)this.RouteData.Values["skip"], out skip) ? skip : 0;
int showPosts = 3;

vm.PageTitle = "Async and wait";

vm.totalPosts = _context.Posts.Where(y => y.PublishedFrom <= DateTime.Now).Count();

vm.Posts = _context.Posts
.Where(y => y.PublishedFrom <= DateTime.Now








public IActionResult Index()


BlogHomeVM vm = new BlogHomeVM();

int skip = int.TryParse((string)this.RouteData.Values["skip"], out skip) ? skip : 0;
int showPosts = 3;

vm.PageTitle = "Async and wait";

vm.totalPosts = _context.Posts.Where(y => y.PublishedFrom <= DateTime.Now).Count();

vm.Posts = _context.Posts
.Where(y => y.PublishedFrom <= DateTime.Now





public IActionResult Index()


BlogHomeVM vm = new BlogHomeVM();

int skip = int.TryParse((string)this.RouteData.Values["skip"], out skip) ? skip : 0;
int showPosts = 3;

vm.PageTitle = "Async and wait";

vm.totalPosts = _context.Posts.Where(y => y.PublishedFrom <= DateTime.Now).Count();

vm.Posts = _context.Posts
.Where(y => y.PublishedFrom <= DateTime.Now






.net unit-testing






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 9 at 10:58









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
    Nov 9 at 11:01










  • Are there rules/suggestions around which parts of the code are suitable for testing and which type, ie unit,integration, system?
    – CerIs
    Nov 9 at 11:02












  • 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
    Nov 9 at 11:01










  • Are there rules/suggestions around which parts of the code are suitable for testing and which type, ie unit,integration, system?
    – CerIs
    Nov 9 at 11:02







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
Nov 9 at 11:01




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
Nov 9 at 11:01












Are there rules/suggestions around which parts of the code are suitable for testing and which type, ie unit,integration, system?
– CerIs
Nov 9 at 11:02




Are there rules/suggestions around which parts of the code are suitable for testing and which type, ie unit,integration, system?
– CerIs
Nov 9 at 11:02












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










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
    Nov 9 at 23:05










  • @Fabio not odd at all, you inject it so you can mock it later. You cannot mock HttpContext property.
    – Sergey Kolodiy
    Nov 12 at 17:22











  • I don't need to mock HttpContext, instead I can create it and inject to the controller. Possible I will need to mock IIdentity to provide expected value for IsAuthenticated.
    – Fabio
    Nov 14 at 18:26










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















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










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
    Nov 9 at 23:05










  • @Fabio not odd at all, you inject it so you can mock it later. You cannot mock HttpContext property.
    – Sergey Kolodiy
    Nov 12 at 17:22











  • I don't need to mock HttpContext, instead I can create it and inject to the controller. Possible I will need to mock IIdentity to provide expected value for IsAuthenticated.
    – Fabio
    Nov 14 at 18:26














up vote
1
down vote



accepted










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
    Nov 9 at 23:05










  • @Fabio not odd at all, you inject it so you can mock it later. You cannot mock HttpContext property.
    – Sergey Kolodiy
    Nov 12 at 17:22











  • I don't need to mock HttpContext, instead I can create it and inject to the controller. Possible I will need to mock IIdentity to provide expected value for IsAuthenticated.
    – Fabio
    Nov 14 at 18:26












up vote
1
down vote



accepted







up vote
1
down vote



accepted






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 Nov 9 at 11:05









Sergey Kolodiy

4,29812345




4,29812345











  • Seems little bid odd injecting IHttpContextAccessor, because controller already have access to the current HttpContext
    – Fabio
    Nov 9 at 23:05










  • @Fabio not odd at all, you inject it so you can mock it later. You cannot mock HttpContext property.
    – Sergey Kolodiy
    Nov 12 at 17:22











  • I don't need to mock HttpContext, instead I can create it and inject to the controller. Possible I will need to mock IIdentity to provide expected value for IsAuthenticated.
    – Fabio
    Nov 14 at 18:26
















  • Seems little bid odd injecting IHttpContextAccessor, because controller already have access to the current HttpContext
    – Fabio
    Nov 9 at 23:05










  • @Fabio not odd at all, you inject it so you can mock it later. You cannot mock HttpContext property.
    – Sergey Kolodiy
    Nov 12 at 17:22











  • I don't need to mock HttpContext, instead I can create it and inject to the controller. Possible I will need to mock IIdentity to provide expected value for IsAuthenticated.
    – Fabio
    Nov 14 at 18:26















Seems little bid odd injecting IHttpContextAccessor, because controller already have access to the current HttpContext
– Fabio
Nov 9 at 23:05




Seems little bid odd injecting IHttpContextAccessor, because controller already have access to the current HttpContext
– Fabio
Nov 9 at 23:05












@Fabio not odd at all, you inject it so you can mock it later. You cannot mock HttpContext property.
– Sergey Kolodiy
Nov 12 at 17:22





@Fabio not odd at all, you inject it so you can mock it later. You cannot mock HttpContext property.
– Sergey Kolodiy
Nov 12 at 17:22













I don't need to mock HttpContext, instead I can create it and inject to the controller. Possible I will need to mock IIdentity to provide expected value for IsAuthenticated.
– Fabio
Nov 14 at 18:26




I don't need to mock HttpContext, instead I can create it and inject to the controller. Possible I will need to mock IIdentity to provide expected value for IsAuthenticated.
– Fabio
Nov 14 at 18:26

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




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















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

PHP code is not being executed, instead code shows on the page

Administrative divisions of China