ruby - Having never written any automated tests, how should I start behaviour-driven development? -
i've been programming years in plenty of languages , think i'm pretty @ it. however, haven't ever written automated testing: no unit tests, no tdd, no bdd, nothing.
i've tried start writing proper test suites projects. can see theoretical value of beingness able test code in project automatically after making changes. can see how test frameworks rspec , mocha should create setting , running said tests reasonably easy, , dsls provide writing tests.
but have never managed write actual unit test part of code. stuff write never seems testable in way that's useful.
functions don't seem callable outside context in they're used. many functions write create http-request calls, or database queries, or other non-easily-testable call. some functions homecoming strings of html. can compare html string against hardcoded version of same string, seems limit ability alter part of code. plus having loads of html in test code mess. i can pass mock/spy objects method, , create sure method calls, far can tell that's testing implementation details of method i'm "testing".how go getting started proper bdd testing? (i'd preferably using mocha , node.js, general advice on bdd fine too.)
it looks main question you're asking is, "how write testable code"?
being fan of object oriented programming know i'm biased, in experience it's far easier test code written in oo style. reason unit tests meant test small, isolated components of system, , designed object oriented code (mostly) provides this.
i agree functions linked context they're in, making them hard test. don't have lot of experience functional programming, know context passed around in sort of variable, making hard separate concerns of functions.
with oo programming, have tested objects wrap around http requests, database queries, etc, mocking object actual network request homecoming known set of data. test wrapper object handles info in right way. can test failures , unexpected data. way of doing setting local server utilize instead of normal endpoint, gives test suite external dependency, should avoided when possible.
when testing html, many people don't @ all, due highly changeable nature of view layer. however, there things worth testing, never total string of html - you've discovered, tiny alter mean whole test breaks. testing in case, 2 strings in separate parts of code base of operations same?
the best thing load html string function/object html parser library, , can utilize xpath or css selectors check tags particular classes, ids or other attributes, , check number of elements match requirements. rspec has built in (the have_tag()
method), many testing libraries.
something else might @ integration testing (e.g. capybara, selenium). load web app javascript engine, can check html elements , javascript events.
on whole mocking/stubbing thing, want objects dependencies of object you're testing. otherwise can pretty much manipulate assert true!
as resources on testing, i'd recommend looking @ test driven development books if don't plan practice tdd. main reason throw head first testing. here few:
kent beck's book test driven development: example free ebook on tdd php, practical php testing this website, art of unit testing slideshare - search unit testing or bdd , read many possible! david chelimsky et. al.: the rspec book ruby node.js rspec bdd mocha
No comments:
Post a Comment