Testing REST style controllers in ASP.Net Core












1















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









share|improve this question























  • 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 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


















1















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









share|improve this question























  • 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 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
















1












1








1








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









share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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





















  • 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 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



















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














1 Answer
1






active

oldest

votes


















1














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.






share|improve this answer


























  • 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











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
});


}
});














draft saved

draft discarded


















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









1














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.






share|improve this answer


























  • 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
















1














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.






share|improve this answer


























  • 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














1












1








1







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.






share|improve this answer















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.







share|improve this answer














share|improve this answer



share|improve this answer








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 property Value), 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













  • 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

















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




















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.




draft saved


draft discarded














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





















































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

Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

A Topological Invariant for $pi_3(U(n))$