Unit Test - Testing API MethodsUnit Testing The Presenter (in an MVP Context) Using Mock Objects?Is this a good simplification of fake object types?xcodebuild does not run any unit tests when launched from consoleTesting a Python REST APIBest Practice of Unit Test Entry PointImproving my Unit testing: How to test simple methods that call internal methods, resulting in complex (yet similar) outputHow to find method name and return types in API testing?How to solve the unit tester's dilemmaUse tested class during test setup and assertion? (Errors cancel out each other…)Looking for examples of scenarios (types of code) which are ideal for unit testing
Can my American children re-enter the USA by International flight with a passport card? Being that their passport book has expired
Will consteval functions allow template parameters dependent on function arguments?
Is random forest for regression a 'true' regression?
How to check if comma list is empty?
How would you translate "grit" (personality trait) to Chinese?
What metal is most suitable for a ladder submerged in an underground water tank?
Assembly writer vs compiler
Was the dragon prowess intentionally downplayed in S08E04?
My bread in my bread maker rises and then falls down just after cooking starts
It is as easy as A B C, Figure out U V C from the given relationship
Why is the Advance Variation considered strong vs the Caro-Kann but not vs the Scandinavian?
Do high-wing aircraft represent more difficult engineering challenges than low-wing aircraft?
Do people who work at research institutes consider themselves "academics"?
What dog breeds survive the apocalypse for generations?
What is the status of the Lannisters after Season 8 Episode 5, "The Bells"?
Why when I add jam to my tea it stops producing thin "membrane" on top?
Could a space colony 1g from the sun work?
Why did the metro bus stop at each railway crossing, despite no warning indicating a train was coming?
How much outgoing traffic would a HTTP load balance use?
Developers demotivated due to working on same project for more than 2 years
Using chord iii in a chord progression (major key)
Does it matter what way the tires go if no directional arrow?
Promotion comes with unexpected 24/7/365 on-call
Is there an academic word that means "to split hairs over"?
Unit Test - Testing API Methods
Unit Testing The Presenter (in an MVP Context) Using Mock Objects?Is this a good simplification of fake object types?xcodebuild does not run any unit tests when launched from consoleTesting a Python REST APIBest Practice of Unit Test Entry PointImproving my Unit testing: How to test simple methods that call internal methods, resulting in complex (yet similar) outputHow to find method name and return types in API testing?How to solve the unit tester's dilemmaUse tested class during test setup and assertion? (Errors cancel out each other…)Looking for examples of scenarios (types of code) which are ideal for unit testing
I was reading the following article
Unit testing an API
This article is about unit testing an API and the methods on a class through the interface. What I do not seem to grasp is the fact that in my idea you would want to test the method implementation / functionality of the 'real' methods?
So If I implement the method 'GetById', I would want to test the functionality of that function. However the Article shows how to create a Fake and test the Fake implementation of 'GetById'. This way you have two different definitions and testing just a 'totally different method / piece of code' and not the implementation / code you would like to see tested in the first place?
Maybe I am missing the point of unit tests here?
c# unit-tests api-testing
New contributor
add a comment |
I was reading the following article
Unit testing an API
This article is about unit testing an API and the methods on a class through the interface. What I do not seem to grasp is the fact that in my idea you would want to test the method implementation / functionality of the 'real' methods?
So If I implement the method 'GetById', I would want to test the functionality of that function. However the Article shows how to create a Fake and test the Fake implementation of 'GetById'. This way you have two different definitions and testing just a 'totally different method / piece of code' and not the implementation / code you would like to see tested in the first place?
Maybe I am missing the point of unit tests here?
c# unit-tests api-testing
New contributor
I didn't read all, but it look like a Mock project to me.There is answer about mocking here link
– Nikolay Barakov
May 10 at 12:36
add a comment |
I was reading the following article
Unit testing an API
This article is about unit testing an API and the methods on a class through the interface. What I do not seem to grasp is the fact that in my idea you would want to test the method implementation / functionality of the 'real' methods?
So If I implement the method 'GetById', I would want to test the functionality of that function. However the Article shows how to create a Fake and test the Fake implementation of 'GetById'. This way you have two different definitions and testing just a 'totally different method / piece of code' and not the implementation / code you would like to see tested in the first place?
Maybe I am missing the point of unit tests here?
c# unit-tests api-testing
New contributor
I was reading the following article
Unit testing an API
This article is about unit testing an API and the methods on a class through the interface. What I do not seem to grasp is the fact that in my idea you would want to test the method implementation / functionality of the 'real' methods?
So If I implement the method 'GetById', I would want to test the functionality of that function. However the Article shows how to create a Fake and test the Fake implementation of 'GetById'. This way you have two different definitions and testing just a 'totally different method / piece of code' and not the implementation / code you would like to see tested in the first place?
Maybe I am missing the point of unit tests here?
c# unit-tests api-testing
c# unit-tests api-testing
New contributor
New contributor
edited May 10 at 14:41
João Farias
3,544419
3,544419
New contributor
asked May 10 at 12:00
Sliver2009Sliver2009
1091
1091
New contributor
New contributor
I didn't read all, but it look like a Mock project to me.There is answer about mocking here link
– Nikolay Barakov
May 10 at 12:36
add a comment |
I didn't read all, but it look like a Mock project to me.There is answer about mocking here link
– Nikolay Barakov
May 10 at 12:36
I didn't read all, but it look like a Mock project to me.There is answer about mocking here link
– Nikolay Barakov
May 10 at 12:36
I didn't read all, but it look like a Mock project to me.There is answer about mocking here link
– Nikolay Barakov
May 10 at 12:36
add a comment |
3 Answers
3
active
oldest
votes
So If I implement the method 'GetById', I would want to test the
functionality of that function.
Sure, and these tests would go in a ShoppingCartServiceTest
class.
However the Article shows how to
create a Fake and test the Fake implementation of 'GetById'.
Observe the test class of the article is ShoppingCartControllerTest
. It is testing the controller, the class contains the endpoints:
// DELETE api/shoppingcart/5
[HttpDelete("id")]
public ActionResult Remove(Guid id)
And this class depends on a service (which fetches the data itself):
var existingItem = _service.GetById(id);
ShoppingCartControllerTest class is not worried about testing if the fetching is correct, but that the Controller calls the service in the correct moments and performs the necessary actions:
// Code this class wants to test
var existingItem = _service.GetById(id);
if (existingItem == null)
return NotFound();
_service.Remove(id);
return Ok();
For this reason, one should create a Fake of the service, putting in control of the test the behavior of the service and allowing test isolation to the behavior of the Controller. The Controller class is designed in order to get the service injected - and this service can be a real implementation or a fake.
public ShoppingCartControllerTest()
_service = new ShoppingCartServiceFake();
_controller = new ShoppingCartController(_service);
Instead of a fake, the test class could have created other types of mocks, e.g. stubs or spies
To test GetById, you would go through a similar process:
- Refactor the Service implementation so it get its dependencies in the constructor;
- Create mocks for the dependencies;
- In the ServiceTest class, instantiate the mocks and inject them in the Service implementation;
- Exercise the Service implementation and checking its behavior, assuming the mock behavior of the dependencies.
OBS: The concerns of the article are not what most people call API Testing. It is really unit testing of class which happens to be the boundary of an API. API Testing usually refers to Integration Testing (or Integrated Testing to be more specific).
add a comment |
The real GetById
probably accesses a database to get the data associated with the ID. In order to prevent the test becoming an end-to-end/integration test you will need to fake a part of your application. Maybe the example is oversimplified.
I would read into a ports and adapter architecture and how to test it in the book Growing Object-Oriented Software Guided by Tests, instead learning from just blogs.
add a comment |
How I'm reading it, they confusingly refer to 2 different bits of code as GetById here.
In the first instance they're referring to this method on the ShoppingCartController:
public ActionResult<ShoppingItem> Get(Guid id)
This is the method that they are unit testing.
In the second instance they're referring to a piece of code that isn't currently under test, this method on the IShoppingCartService
ShoppingItem GetById(Guid id);
As Niels says, this method on the IShoppingCartService is mocked/faked because it's outside the scope of a unit test.
I think the confusion when reading this page comes from the subheading "Testing the GetById method". What they really appear to mean is "Testing the Get(Guid) method". As you mention, there would be no point in testing the GetById method from the service as it is fake.
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "244"
;
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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
);
);
Sliver2009 is a new contributor. Be nice, and check out our Code of Conduct.
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%2fsqa.stackexchange.com%2fquestions%2f39113%2funit-test-testing-api-methods%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
So If I implement the method 'GetById', I would want to test the
functionality of that function.
Sure, and these tests would go in a ShoppingCartServiceTest
class.
However the Article shows how to
create a Fake and test the Fake implementation of 'GetById'.
Observe the test class of the article is ShoppingCartControllerTest
. It is testing the controller, the class contains the endpoints:
// DELETE api/shoppingcart/5
[HttpDelete("id")]
public ActionResult Remove(Guid id)
And this class depends on a service (which fetches the data itself):
var existingItem = _service.GetById(id);
ShoppingCartControllerTest class is not worried about testing if the fetching is correct, but that the Controller calls the service in the correct moments and performs the necessary actions:
// Code this class wants to test
var existingItem = _service.GetById(id);
if (existingItem == null)
return NotFound();
_service.Remove(id);
return Ok();
For this reason, one should create a Fake of the service, putting in control of the test the behavior of the service and allowing test isolation to the behavior of the Controller. The Controller class is designed in order to get the service injected - and this service can be a real implementation or a fake.
public ShoppingCartControllerTest()
_service = new ShoppingCartServiceFake();
_controller = new ShoppingCartController(_service);
Instead of a fake, the test class could have created other types of mocks, e.g. stubs or spies
To test GetById, you would go through a similar process:
- Refactor the Service implementation so it get its dependencies in the constructor;
- Create mocks for the dependencies;
- In the ServiceTest class, instantiate the mocks and inject them in the Service implementation;
- Exercise the Service implementation and checking its behavior, assuming the mock behavior of the dependencies.
OBS: The concerns of the article are not what most people call API Testing. It is really unit testing of class which happens to be the boundary of an API. API Testing usually refers to Integration Testing (or Integrated Testing to be more specific).
add a comment |
So If I implement the method 'GetById', I would want to test the
functionality of that function.
Sure, and these tests would go in a ShoppingCartServiceTest
class.
However the Article shows how to
create a Fake and test the Fake implementation of 'GetById'.
Observe the test class of the article is ShoppingCartControllerTest
. It is testing the controller, the class contains the endpoints:
// DELETE api/shoppingcart/5
[HttpDelete("id")]
public ActionResult Remove(Guid id)
And this class depends on a service (which fetches the data itself):
var existingItem = _service.GetById(id);
ShoppingCartControllerTest class is not worried about testing if the fetching is correct, but that the Controller calls the service in the correct moments and performs the necessary actions:
// Code this class wants to test
var existingItem = _service.GetById(id);
if (existingItem == null)
return NotFound();
_service.Remove(id);
return Ok();
For this reason, one should create a Fake of the service, putting in control of the test the behavior of the service and allowing test isolation to the behavior of the Controller. The Controller class is designed in order to get the service injected - and this service can be a real implementation or a fake.
public ShoppingCartControllerTest()
_service = new ShoppingCartServiceFake();
_controller = new ShoppingCartController(_service);
Instead of a fake, the test class could have created other types of mocks, e.g. stubs or spies
To test GetById, you would go through a similar process:
- Refactor the Service implementation so it get its dependencies in the constructor;
- Create mocks for the dependencies;
- In the ServiceTest class, instantiate the mocks and inject them in the Service implementation;
- Exercise the Service implementation and checking its behavior, assuming the mock behavior of the dependencies.
OBS: The concerns of the article are not what most people call API Testing. It is really unit testing of class which happens to be the boundary of an API. API Testing usually refers to Integration Testing (or Integrated Testing to be more specific).
add a comment |
So If I implement the method 'GetById', I would want to test the
functionality of that function.
Sure, and these tests would go in a ShoppingCartServiceTest
class.
However the Article shows how to
create a Fake and test the Fake implementation of 'GetById'.
Observe the test class of the article is ShoppingCartControllerTest
. It is testing the controller, the class contains the endpoints:
// DELETE api/shoppingcart/5
[HttpDelete("id")]
public ActionResult Remove(Guid id)
And this class depends on a service (which fetches the data itself):
var existingItem = _service.GetById(id);
ShoppingCartControllerTest class is not worried about testing if the fetching is correct, but that the Controller calls the service in the correct moments and performs the necessary actions:
// Code this class wants to test
var existingItem = _service.GetById(id);
if (existingItem == null)
return NotFound();
_service.Remove(id);
return Ok();
For this reason, one should create a Fake of the service, putting in control of the test the behavior of the service and allowing test isolation to the behavior of the Controller. The Controller class is designed in order to get the service injected - and this service can be a real implementation or a fake.
public ShoppingCartControllerTest()
_service = new ShoppingCartServiceFake();
_controller = new ShoppingCartController(_service);
Instead of a fake, the test class could have created other types of mocks, e.g. stubs or spies
To test GetById, you would go through a similar process:
- Refactor the Service implementation so it get its dependencies in the constructor;
- Create mocks for the dependencies;
- In the ServiceTest class, instantiate the mocks and inject them in the Service implementation;
- Exercise the Service implementation and checking its behavior, assuming the mock behavior of the dependencies.
OBS: The concerns of the article are not what most people call API Testing. It is really unit testing of class which happens to be the boundary of an API. API Testing usually refers to Integration Testing (or Integrated Testing to be more specific).
So If I implement the method 'GetById', I would want to test the
functionality of that function.
Sure, and these tests would go in a ShoppingCartServiceTest
class.
However the Article shows how to
create a Fake and test the Fake implementation of 'GetById'.
Observe the test class of the article is ShoppingCartControllerTest
. It is testing the controller, the class contains the endpoints:
// DELETE api/shoppingcart/5
[HttpDelete("id")]
public ActionResult Remove(Guid id)
And this class depends on a service (which fetches the data itself):
var existingItem = _service.GetById(id);
ShoppingCartControllerTest class is not worried about testing if the fetching is correct, but that the Controller calls the service in the correct moments and performs the necessary actions:
// Code this class wants to test
var existingItem = _service.GetById(id);
if (existingItem == null)
return NotFound();
_service.Remove(id);
return Ok();
For this reason, one should create a Fake of the service, putting in control of the test the behavior of the service and allowing test isolation to the behavior of the Controller. The Controller class is designed in order to get the service injected - and this service can be a real implementation or a fake.
public ShoppingCartControllerTest()
_service = new ShoppingCartServiceFake();
_controller = new ShoppingCartController(_service);
Instead of a fake, the test class could have created other types of mocks, e.g. stubs or spies
To test GetById, you would go through a similar process:
- Refactor the Service implementation so it get its dependencies in the constructor;
- Create mocks for the dependencies;
- In the ServiceTest class, instantiate the mocks and inject them in the Service implementation;
- Exercise the Service implementation and checking its behavior, assuming the mock behavior of the dependencies.
OBS: The concerns of the article are not what most people call API Testing. It is really unit testing of class which happens to be the boundary of an API. API Testing usually refers to Integration Testing (or Integrated Testing to be more specific).
edited May 10 at 14:46
answered May 10 at 14:41
João FariasJoão Farias
3,544419
3,544419
add a comment |
add a comment |
The real GetById
probably accesses a database to get the data associated with the ID. In order to prevent the test becoming an end-to-end/integration test you will need to fake a part of your application. Maybe the example is oversimplified.
I would read into a ports and adapter architecture and how to test it in the book Growing Object-Oriented Software Guided by Tests, instead learning from just blogs.
add a comment |
The real GetById
probably accesses a database to get the data associated with the ID. In order to prevent the test becoming an end-to-end/integration test you will need to fake a part of your application. Maybe the example is oversimplified.
I would read into a ports and adapter architecture and how to test it in the book Growing Object-Oriented Software Guided by Tests, instead learning from just blogs.
add a comment |
The real GetById
probably accesses a database to get the data associated with the ID. In order to prevent the test becoming an end-to-end/integration test you will need to fake a part of your application. Maybe the example is oversimplified.
I would read into a ports and adapter architecture and how to test it in the book Growing Object-Oriented Software Guided by Tests, instead learning from just blogs.
The real GetById
probably accesses a database to get the data associated with the ID. In order to prevent the test becoming an end-to-end/integration test you will need to fake a part of your application. Maybe the example is oversimplified.
I would read into a ports and adapter architecture and how to test it in the book Growing Object-Oriented Software Guided by Tests, instead learning from just blogs.
answered May 10 at 12:52
Niels van ReijmersdalNiels van Reijmersdal
22.4k23177
22.4k23177
add a comment |
add a comment |
How I'm reading it, they confusingly refer to 2 different bits of code as GetById here.
In the first instance they're referring to this method on the ShoppingCartController:
public ActionResult<ShoppingItem> Get(Guid id)
This is the method that they are unit testing.
In the second instance they're referring to a piece of code that isn't currently under test, this method on the IShoppingCartService
ShoppingItem GetById(Guid id);
As Niels says, this method on the IShoppingCartService is mocked/faked because it's outside the scope of a unit test.
I think the confusion when reading this page comes from the subheading "Testing the GetById method". What they really appear to mean is "Testing the Get(Guid) method". As you mention, there would be no point in testing the GetById method from the service as it is fake.
add a comment |
How I'm reading it, they confusingly refer to 2 different bits of code as GetById here.
In the first instance they're referring to this method on the ShoppingCartController:
public ActionResult<ShoppingItem> Get(Guid id)
This is the method that they are unit testing.
In the second instance they're referring to a piece of code that isn't currently under test, this method on the IShoppingCartService
ShoppingItem GetById(Guid id);
As Niels says, this method on the IShoppingCartService is mocked/faked because it's outside the scope of a unit test.
I think the confusion when reading this page comes from the subheading "Testing the GetById method". What they really appear to mean is "Testing the Get(Guid) method". As you mention, there would be no point in testing the GetById method from the service as it is fake.
add a comment |
How I'm reading it, they confusingly refer to 2 different bits of code as GetById here.
In the first instance they're referring to this method on the ShoppingCartController:
public ActionResult<ShoppingItem> Get(Guid id)
This is the method that they are unit testing.
In the second instance they're referring to a piece of code that isn't currently under test, this method on the IShoppingCartService
ShoppingItem GetById(Guid id);
As Niels says, this method on the IShoppingCartService is mocked/faked because it's outside the scope of a unit test.
I think the confusion when reading this page comes from the subheading "Testing the GetById method". What they really appear to mean is "Testing the Get(Guid) method". As you mention, there would be no point in testing the GetById method from the service as it is fake.
How I'm reading it, they confusingly refer to 2 different bits of code as GetById here.
In the first instance they're referring to this method on the ShoppingCartController:
public ActionResult<ShoppingItem> Get(Guid id)
This is the method that they are unit testing.
In the second instance they're referring to a piece of code that isn't currently under test, this method on the IShoppingCartService
ShoppingItem GetById(Guid id);
As Niels says, this method on the IShoppingCartService is mocked/faked because it's outside the scope of a unit test.
I think the confusion when reading this page comes from the subheading "Testing the GetById method". What they really appear to mean is "Testing the Get(Guid) method". As you mention, there would be no point in testing the GetById method from the service as it is fake.
edited May 10 at 14:31
answered May 10 at 13:28
anonygooseanonygoose
3147
3147
add a comment |
add a comment |
Sliver2009 is a new contributor. Be nice, and check out our Code of Conduct.
Sliver2009 is a new contributor. Be nice, and check out our Code of Conduct.
Sliver2009 is a new contributor. Be nice, and check out our Code of Conduct.
Sliver2009 is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Software Quality Assurance & Testing Stack Exchange!
- 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%2fsqa.stackexchange.com%2fquestions%2f39113%2funit-test-testing-api-methods%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
I didn't read all, but it look like a Mock project to me.There is answer about mocking here link
– Nikolay Barakov
May 10 at 12:36