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()
.net unit-testing
add a comment |
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()
.net unit-testing
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
add a comment |
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()
.net unit-testing
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
.net unit-testing
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
add a comment |
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
add a comment |
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
Seems little bid odd injectingIHttpContextAccessor, because controller already have access to the currentHttpContext
– Fabio
yesterday
add a comment |
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
Seems little bid odd injectingIHttpContextAccessor, because controller already have access to the currentHttpContext
– Fabio
yesterday
add a comment |
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
Seems little bid odd injectingIHttpContextAccessor, because controller already have access to the currentHttpContext
– Fabio
yesterday
add a comment |
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
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
answered yesterday
Sergey Kolodiy
4,26312344
4,26312344
Seems little bid odd injectingIHttpContextAccessor, because controller already have access to the currentHttpContext
– Fabio
yesterday
add a comment |
Seems little bid odd injectingIHttpContextAccessor, because controller already have access to the currentHttpContext
– 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
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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