Testing REST style controllers in ASP.Net Core
I am trying to unit test the action method in asp.net core project. The test is failing when I expect it to succeed. Seems that the problem is due to the return type of the Action method.
I have also tried testing a method in BusinessLogic with return type as 'IEnumerable', which runs as expected. Here is the code that I am trying.
Controller/Action method:
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
private IValueLogic _objValueLogic;
public ValuesController(IValueLogic objValueLogic) {
_objValueLogic = objValueLogic;
}
// GET api/values
[HttpGet]
public ActionResult<IEnumerable<string>> Get()
{
IEnumerable<string> allValues = _objValueLogic.GetAll();
return new ActionResult<IEnumerable<string>>(allValues);
}
}
Test class:
public class ValueApiTest
{
private ValuesController _objValuesController;
public ValueApiTest() {
Mock<IValueLogic> mockValueLogic = new Mock<IValueLogic>();
mockValueLogic.Setup(x => x.GetAll()).Returns(new string {"Value1", "Value2"});
_objValuesController = new ValuesController(mockValueLogic.Object);
}
[Fact]
public void GetAll_Success() {
ActionResult<IEnumerable<string>> expected = new ActionResult<IEnumerable<string>>(new string {"Value1", "Value2"});
ActionResult<IEnumerable<string>> actual = _objValuesController.Get();
Assert.Equal<ActionResult<IEnumerable<string>>>(expected, actual);
}
}
I want a success result as both expected and actual values are equal, but it fails with message. As you can see the Expected and Actual values are printed the same.
Starting test execution, please wait...
[xUnit.net 00:00:03.01] Test.WebApi.ValueApiTest.GetAll_Success [FAIL]
Failed Test.WebApi.ValueApiTest.GetAll_Success
Error Message:
Assert.Equal() Failure
Expected: ActionResult`1 { Result = null, Value = ["Value1", "Value2"] }
Actual: ActionResult`1 { Result = null, Value = ["Value1", "Value2"] }
Stack Trace:
at Test.WebApi.ValueApiTest.GetAll_Success() in /home/saurabh/DevEnv/DotNetCore/dotnet-template/Test/Test.WebApi/ValueApiTest.cs:line 23
Total tests: 2. Passed: 1. Failed: 1. Skipped: 0.
Test Run Failed.
Test execution time: 4.5573 Seconds
c# asp.net-core xunit
add a comment |
I am trying to unit test the action method in asp.net core project. The test is failing when I expect it to succeed. Seems that the problem is due to the return type of the Action method.
I have also tried testing a method in BusinessLogic with return type as 'IEnumerable', which runs as expected. Here is the code that I am trying.
Controller/Action method:
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
private IValueLogic _objValueLogic;
public ValuesController(IValueLogic objValueLogic) {
_objValueLogic = objValueLogic;
}
// GET api/values
[HttpGet]
public ActionResult<IEnumerable<string>> Get()
{
IEnumerable<string> allValues = _objValueLogic.GetAll();
return new ActionResult<IEnumerable<string>>(allValues);
}
}
Test class:
public class ValueApiTest
{
private ValuesController _objValuesController;
public ValueApiTest() {
Mock<IValueLogic> mockValueLogic = new Mock<IValueLogic>();
mockValueLogic.Setup(x => x.GetAll()).Returns(new string {"Value1", "Value2"});
_objValuesController = new ValuesController(mockValueLogic.Object);
}
[Fact]
public void GetAll_Success() {
ActionResult<IEnumerable<string>> expected = new ActionResult<IEnumerable<string>>(new string {"Value1", "Value2"});
ActionResult<IEnumerable<string>> actual = _objValuesController.Get();
Assert.Equal<ActionResult<IEnumerable<string>>>(expected, actual);
}
}
I want a success result as both expected and actual values are equal, but it fails with message. As you can see the Expected and Actual values are printed the same.
Starting test execution, please wait...
[xUnit.net 00:00:03.01] Test.WebApi.ValueApiTest.GetAll_Success [FAIL]
Failed Test.WebApi.ValueApiTest.GetAll_Success
Error Message:
Assert.Equal() Failure
Expected: ActionResult`1 { Result = null, Value = ["Value1", "Value2"] }
Actual: ActionResult`1 { Result = null, Value = ["Value1", "Value2"] }
Stack Trace:
at Test.WebApi.ValueApiTest.GetAll_Success() in /home/saurabh/DevEnv/DotNetCore/dotnet-template/Test/Test.WebApi/ValueApiTest.cs:line 23
Total tests: 2. Passed: 1. Failed: 1. Skipped: 0.
Test Run Failed.
Test execution time: 4.5573 Seconds
c# asp.net-core xunit
You could check the type of both expected and actual in debug mode
– Bhawna Jain
Jan 2 at 6:08
Type is the same. I have explicitly mentioned types in the code.
– Saurabh Harwande
Jan 2 at 6:18
2
I believeAssert.Equal
is testing the equality of theActionResult
reference, which will be false (they are two different objects). If you want to test the equality of the arrays then you need toAssert.Equal<IEnumerable<string>>(expected.Value, actual.Value)
– Simply Ged
Jan 2 at 6:25
add a comment |
I am trying to unit test the action method in asp.net core project. The test is failing when I expect it to succeed. Seems that the problem is due to the return type of the Action method.
I have also tried testing a method in BusinessLogic with return type as 'IEnumerable', which runs as expected. Here is the code that I am trying.
Controller/Action method:
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
private IValueLogic _objValueLogic;
public ValuesController(IValueLogic objValueLogic) {
_objValueLogic = objValueLogic;
}
// GET api/values
[HttpGet]
public ActionResult<IEnumerable<string>> Get()
{
IEnumerable<string> allValues = _objValueLogic.GetAll();
return new ActionResult<IEnumerable<string>>(allValues);
}
}
Test class:
public class ValueApiTest
{
private ValuesController _objValuesController;
public ValueApiTest() {
Mock<IValueLogic> mockValueLogic = new Mock<IValueLogic>();
mockValueLogic.Setup(x => x.GetAll()).Returns(new string {"Value1", "Value2"});
_objValuesController = new ValuesController(mockValueLogic.Object);
}
[Fact]
public void GetAll_Success() {
ActionResult<IEnumerable<string>> expected = new ActionResult<IEnumerable<string>>(new string {"Value1", "Value2"});
ActionResult<IEnumerable<string>> actual = _objValuesController.Get();
Assert.Equal<ActionResult<IEnumerable<string>>>(expected, actual);
}
}
I want a success result as both expected and actual values are equal, but it fails with message. As you can see the Expected and Actual values are printed the same.
Starting test execution, please wait...
[xUnit.net 00:00:03.01] Test.WebApi.ValueApiTest.GetAll_Success [FAIL]
Failed Test.WebApi.ValueApiTest.GetAll_Success
Error Message:
Assert.Equal() Failure
Expected: ActionResult`1 { Result = null, Value = ["Value1", "Value2"] }
Actual: ActionResult`1 { Result = null, Value = ["Value1", "Value2"] }
Stack Trace:
at Test.WebApi.ValueApiTest.GetAll_Success() in /home/saurabh/DevEnv/DotNetCore/dotnet-template/Test/Test.WebApi/ValueApiTest.cs:line 23
Total tests: 2. Passed: 1. Failed: 1. Skipped: 0.
Test Run Failed.
Test execution time: 4.5573 Seconds
c# asp.net-core xunit
I am trying to unit test the action method in asp.net core project. The test is failing when I expect it to succeed. Seems that the problem is due to the return type of the Action method.
I have also tried testing a method in BusinessLogic with return type as 'IEnumerable', which runs as expected. Here is the code that I am trying.
Controller/Action method:
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
private IValueLogic _objValueLogic;
public ValuesController(IValueLogic objValueLogic) {
_objValueLogic = objValueLogic;
}
// GET api/values
[HttpGet]
public ActionResult<IEnumerable<string>> Get()
{
IEnumerable<string> allValues = _objValueLogic.GetAll();
return new ActionResult<IEnumerable<string>>(allValues);
}
}
Test class:
public class ValueApiTest
{
private ValuesController _objValuesController;
public ValueApiTest() {
Mock<IValueLogic> mockValueLogic = new Mock<IValueLogic>();
mockValueLogic.Setup(x => x.GetAll()).Returns(new string {"Value1", "Value2"});
_objValuesController = new ValuesController(mockValueLogic.Object);
}
[Fact]
public void GetAll_Success() {
ActionResult<IEnumerable<string>> expected = new ActionResult<IEnumerable<string>>(new string {"Value1", "Value2"});
ActionResult<IEnumerable<string>> actual = _objValuesController.Get();
Assert.Equal<ActionResult<IEnumerable<string>>>(expected, actual);
}
}
I want a success result as both expected and actual values are equal, but it fails with message. As you can see the Expected and Actual values are printed the same.
Starting test execution, please wait...
[xUnit.net 00:00:03.01] Test.WebApi.ValueApiTest.GetAll_Success [FAIL]
Failed Test.WebApi.ValueApiTest.GetAll_Success
Error Message:
Assert.Equal() Failure
Expected: ActionResult`1 { Result = null, Value = ["Value1", "Value2"] }
Actual: ActionResult`1 { Result = null, Value = ["Value1", "Value2"] }
Stack Trace:
at Test.WebApi.ValueApiTest.GetAll_Success() in /home/saurabh/DevEnv/DotNetCore/dotnet-template/Test/Test.WebApi/ValueApiTest.cs:line 23
Total tests: 2. Passed: 1. Failed: 1. Skipped: 0.
Test Run Failed.
Test execution time: 4.5573 Seconds
c# asp.net-core xunit
c# asp.net-core xunit
asked Jan 2 at 6:03
Saurabh HarwandeSaurabh Harwande
108413
108413
You could check the type of both expected and actual in debug mode
– Bhawna Jain
Jan 2 at 6:08
Type is the same. I have explicitly mentioned types in the code.
– Saurabh Harwande
Jan 2 at 6:18
2
I believeAssert.Equal
is testing the equality of theActionResult
reference, which will be false (they are two different objects). If you want to test the equality of the arrays then you need toAssert.Equal<IEnumerable<string>>(expected.Value, actual.Value)
– Simply Ged
Jan 2 at 6:25
add a comment |
You could check the type of both expected and actual in debug mode
– Bhawna Jain
Jan 2 at 6:08
Type is the same. I have explicitly mentioned types in the code.
– Saurabh Harwande
Jan 2 at 6:18
2
I believeAssert.Equal
is testing the equality of theActionResult
reference, which will be false (they are two different objects). If you want to test the equality of the arrays then you need toAssert.Equal<IEnumerable<string>>(expected.Value, actual.Value)
– Simply Ged
Jan 2 at 6:25
You could check the type of both expected and actual in debug mode
– Bhawna Jain
Jan 2 at 6:08
You could check the type of both expected and actual in debug mode
– Bhawna Jain
Jan 2 at 6:08
Type is the same. I have explicitly mentioned types in the code.
– Saurabh Harwande
Jan 2 at 6:18
Type is the same. I have explicitly mentioned types in the code.
– Saurabh Harwande
Jan 2 at 6:18
2
2
I believe
Assert.Equal
is testing the equality of the ActionResult
reference, which will be false (they are two different objects). If you want to test the equality of the arrays then you need to Assert.Equal<IEnumerable<string>>(expected.Value, actual.Value)
– Simply Ged
Jan 2 at 6:25
I believe
Assert.Equal
is testing the equality of the ActionResult
reference, which will be false (they are two different objects). If you want to test the equality of the arrays then you need to Assert.Equal<IEnumerable<string>>(expected.Value, actual.Value)
– Simply Ged
Jan 2 at 6:25
add a comment |
1 Answer
1
active
oldest
votes
You are comparing two reference types, which will be the same if they refer to the same object, which they do not in this case. You can try something like this instead:
public class ValueApiTest
{
private ValuesController _objValuesController;
private string _expected = new string {"Value1", "Value2"};
public ValueApiTest() {
_expected = Mock<IValueLogic> mockValueLogic = new Mock<IValueLogic>();
mockValueLogic.Setup(x => x.GetAll()).Returns(expected);
_objValuesController = new ValuesController(mockValueLogic.Object);
}
[Fact]
public void GetAll_Success() {
IEnumerable<string> actual = _objValuesController.Get().Value;
Assert.Equal<IEnumerable<string>>(expected, actual);
}
}
Of course, this test will not break if your controller return some other object than an ActionResult. If you want to cover that, you can change it to
[Fact]
public void GetAll_Success() {
ActionResult<IEnumerable<string>> result = _objValuesController.Get();
IEnumerable<string> actual = result.Value;
Assert.Equal<IEnumerable<string>>(expected, actual);
}
Now the test will fail if anything else than an ActionResult<IEnumerable<string>>
is returned by your controller.
Yup. I solved it in the same fashion a few minutes after posting the question. But have a question. Is it ok to compare ActionResult.Value instead of ActionResult itself?
– Saurabh Harwande
Jan 2 at 6:38
I'm not sure why it would not be ok... If you want your test to break if the controller returns some other class (that still has the propertyValue
), you can easily change it, so you cover everything that your original test tested for.
– oerkelens
Jan 2 at 6:43
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54001913%2ftesting-rest-style-controllers-in-asp-net-core%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
You are comparing two reference types, which will be the same if they refer to the same object, which they do not in this case. You can try something like this instead:
public class ValueApiTest
{
private ValuesController _objValuesController;
private string _expected = new string {"Value1", "Value2"};
public ValueApiTest() {
_expected = Mock<IValueLogic> mockValueLogic = new Mock<IValueLogic>();
mockValueLogic.Setup(x => x.GetAll()).Returns(expected);
_objValuesController = new ValuesController(mockValueLogic.Object);
}
[Fact]
public void GetAll_Success() {
IEnumerable<string> actual = _objValuesController.Get().Value;
Assert.Equal<IEnumerable<string>>(expected, actual);
}
}
Of course, this test will not break if your controller return some other object than an ActionResult. If you want to cover that, you can change it to
[Fact]
public void GetAll_Success() {
ActionResult<IEnumerable<string>> result = _objValuesController.Get();
IEnumerable<string> actual = result.Value;
Assert.Equal<IEnumerable<string>>(expected, actual);
}
Now the test will fail if anything else than an ActionResult<IEnumerable<string>>
is returned by your controller.
Yup. I solved it in the same fashion a few minutes after posting the question. But have a question. Is it ok to compare ActionResult.Value instead of ActionResult itself?
– Saurabh Harwande
Jan 2 at 6:38
I'm not sure why it would not be ok... If you want your test to break if the controller returns some other class (that still has the propertyValue
), you can easily change it, so you cover everything that your original test tested for.
– oerkelens
Jan 2 at 6:43
add a comment |
You are comparing two reference types, which will be the same if they refer to the same object, which they do not in this case. You can try something like this instead:
public class ValueApiTest
{
private ValuesController _objValuesController;
private string _expected = new string {"Value1", "Value2"};
public ValueApiTest() {
_expected = Mock<IValueLogic> mockValueLogic = new Mock<IValueLogic>();
mockValueLogic.Setup(x => x.GetAll()).Returns(expected);
_objValuesController = new ValuesController(mockValueLogic.Object);
}
[Fact]
public void GetAll_Success() {
IEnumerable<string> actual = _objValuesController.Get().Value;
Assert.Equal<IEnumerable<string>>(expected, actual);
}
}
Of course, this test will not break if your controller return some other object than an ActionResult. If you want to cover that, you can change it to
[Fact]
public void GetAll_Success() {
ActionResult<IEnumerable<string>> result = _objValuesController.Get();
IEnumerable<string> actual = result.Value;
Assert.Equal<IEnumerable<string>>(expected, actual);
}
Now the test will fail if anything else than an ActionResult<IEnumerable<string>>
is returned by your controller.
Yup. I solved it in the same fashion a few minutes after posting the question. But have a question. Is it ok to compare ActionResult.Value instead of ActionResult itself?
– Saurabh Harwande
Jan 2 at 6:38
I'm not sure why it would not be ok... If you want your test to break if the controller returns some other class (that still has the propertyValue
), you can easily change it, so you cover everything that your original test tested for.
– oerkelens
Jan 2 at 6:43
add a comment |
You are comparing two reference types, which will be the same if they refer to the same object, which they do not in this case. You can try something like this instead:
public class ValueApiTest
{
private ValuesController _objValuesController;
private string _expected = new string {"Value1", "Value2"};
public ValueApiTest() {
_expected = Mock<IValueLogic> mockValueLogic = new Mock<IValueLogic>();
mockValueLogic.Setup(x => x.GetAll()).Returns(expected);
_objValuesController = new ValuesController(mockValueLogic.Object);
}
[Fact]
public void GetAll_Success() {
IEnumerable<string> actual = _objValuesController.Get().Value;
Assert.Equal<IEnumerable<string>>(expected, actual);
}
}
Of course, this test will not break if your controller return some other object than an ActionResult. If you want to cover that, you can change it to
[Fact]
public void GetAll_Success() {
ActionResult<IEnumerable<string>> result = _objValuesController.Get();
IEnumerable<string> actual = result.Value;
Assert.Equal<IEnumerable<string>>(expected, actual);
}
Now the test will fail if anything else than an ActionResult<IEnumerable<string>>
is returned by your controller.
You are comparing two reference types, which will be the same if they refer to the same object, which they do not in this case. You can try something like this instead:
public class ValueApiTest
{
private ValuesController _objValuesController;
private string _expected = new string {"Value1", "Value2"};
public ValueApiTest() {
_expected = Mock<IValueLogic> mockValueLogic = new Mock<IValueLogic>();
mockValueLogic.Setup(x => x.GetAll()).Returns(expected);
_objValuesController = new ValuesController(mockValueLogic.Object);
}
[Fact]
public void GetAll_Success() {
IEnumerable<string> actual = _objValuesController.Get().Value;
Assert.Equal<IEnumerable<string>>(expected, actual);
}
}
Of course, this test will not break if your controller return some other object than an ActionResult. If you want to cover that, you can change it to
[Fact]
public void GetAll_Success() {
ActionResult<IEnumerable<string>> result = _objValuesController.Get();
IEnumerable<string> actual = result.Value;
Assert.Equal<IEnumerable<string>>(expected, actual);
}
Now the test will fail if anything else than an ActionResult<IEnumerable<string>>
is returned by your controller.
edited Jan 2 at 6:46
answered Jan 2 at 6:25
oerkelensoerkelens
3,73011425
3,73011425
Yup. I solved it in the same fashion a few minutes after posting the question. But have a question. Is it ok to compare ActionResult.Value instead of ActionResult itself?
– Saurabh Harwande
Jan 2 at 6:38
I'm not sure why it would not be ok... If you want your test to break if the controller returns some other class (that still has the propertyValue
), you can easily change it, so you cover everything that your original test tested for.
– oerkelens
Jan 2 at 6:43
add a comment |
Yup. I solved it in the same fashion a few minutes after posting the question. But have a question. Is it ok to compare ActionResult.Value instead of ActionResult itself?
– Saurabh Harwande
Jan 2 at 6:38
I'm not sure why it would not be ok... If you want your test to break if the controller returns some other class (that still has the propertyValue
), you can easily change it, so you cover everything that your original test tested for.
– oerkelens
Jan 2 at 6:43
Yup. I solved it in the same fashion a few minutes after posting the question. But have a question. Is it ok to compare ActionResult.Value instead of ActionResult itself?
– Saurabh Harwande
Jan 2 at 6:38
Yup. I solved it in the same fashion a few minutes after posting the question. But have a question. Is it ok to compare ActionResult.Value instead of ActionResult itself?
– Saurabh Harwande
Jan 2 at 6:38
I'm not sure why it would not be ok... If you want your test to break if the controller returns some other class (that still has the property
Value
), you can easily change it, so you cover everything that your original test tested for.– oerkelens
Jan 2 at 6:43
I'm not sure why it would not be ok... If you want your test to break if the controller returns some other class (that still has the property
Value
), you can easily change it, so you cover everything that your original test tested for.– oerkelens
Jan 2 at 6:43
add a comment |
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.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54001913%2ftesting-rest-style-controllers-in-asp-net-core%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
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
You could check the type of both expected and actual in debug mode
– Bhawna Jain
Jan 2 at 6:08
Type is the same. I have explicitly mentioned types in the code.
– Saurabh Harwande
Jan 2 at 6:18
2
I believe
Assert.Equal
is testing the equality of theActionResult
reference, which will be false (they are two different objects). If you want to test the equality of the arrays then you need toAssert.Equal<IEnumerable<string>>(expected.Value, actual.Value)
– Simply Ged
Jan 2 at 6:25