Best practices for TDD BDD with code that uses external services / api

I use twitter gem, which basically accesses twitter and allows me to capture tweets, timeline, etc. This is really good, but I have a lot of my code that uses the material that it returns, and I need to test it. What the gem returns is not quite simple lines, there are quite complex objects (also scary), so I left scratches on my head.

So, basically I'm looking for an answer, a book, a blog, an open source project that can show me the rights and errors of testing external services.

answers that are either not language oriented or rubies / rails that would be greatly appreciated.

+4
source share
2 answers

What you are really talking about are two different types of testing that you would like to perform - unit tests and integration tests.

Unit tests will verify the validity of methods regardless of any external data. You should learn some mocking structure based on any language you use. You basically want to say that with tests, something is equivalent to "if these assumptions are qualified, then this test should give ..." The creation frame will determine your assumptions, saying that certain classes / objects are set in a special way and can be considered valid. These are tests that will not depend on the fact that Twitter is alive, or the library / library of the third part that responds.

Integration tests will conduct live tests with the data source, consuming the library / API to perform valid actions. Where it becomes difficult, because you use a third-party service, write to the service (i.e., when creating new tweets). If so, you can always create an account on Twitter that can only be used for write operations. Typically, if you tested a local database β€” for example, you could instead use transactions to test similar operations; rollback transactions instead of committing them.

Here are a few high-level non-linguistic definitions:

I am from the .NET stack, so I won’t pretend to know much about Ruby. However, a quick Google search showed the following:

+6
source

You can easily drown at the http level using something like wiremock http://wiremock.org/ I have used this in several projects now, and it is quite powerful and fast. This will eliminate all the mockery-based code that is set up - just run the jar with the associated mappings and bob your uncle.

0
source

Source: https://habr.com/ru/post/1311484/


All Articles