所有这些只是我对这个广泛而开放的话题的个人看法。有些人可能不同意,而且他们可以很有趣。
作为一般指南,您应该使用Cucumber来测试整个应用程序堆栈,这就是所谓的用户体验。此应用程序堆栈可能由许多较小的独立对象组成,但是,就像使用您的应用程序的用户不会对这些细节感兴趣一样,您的黄瓜测试不应该关心它们,而是关注应用程序的外层。
另一方面,RSpec(在您的设置中! )应该主要关注那些小对象,即应用程序的构建块。
书中的小应用示例存在一个大问题:它们太小了!
Boarder between outer layer of your application and its interiors is to blurred. Entire application is build with two objects! It is hard to distinguished what should test what. As your application grow in size its getting more obvious what is a user experience test(cucumber) and what is object - state test/message expectation test(RSpec).
Using your second example:
With this Cucumber story:
Given I am at the login page
When I fill in the right username and password
Then I should be at the index page
Rspec:
You will probably have some sort of User model:
- test user name syntax(must start with capital for example)
- test password must be 7 characters have numbers etc...
You can have some sort of authentication object:
- test for valid and invalid logins etc
- test for exceptions being thrown ....
What if your Authentication database is on different server?
- mock connection and authentication database and test if database receive an request from authentication object.
And yada yada yada... Forever your cucumber test will guard general purpose of your application user login. Even when you add or change behaviour, under the hood, as long as this test pass you can be confident that user can login.
- you should test things once(in one place)
- object its responsible for testing its incoming massage(object public interface) for state(return value)
- when your object depends on other object(sends its a message) don't test it for state(return value) that object is responsible for it.
- when your object depends on other object(sends its a message) mock that second object and test if it receive your message.
Generally your question is too broad and you wont find a single answer, different people will have different ideas. What you should focus on is to test, as good as you can and with time you will definitely find right way for you. Not a great test suit is much better then none.
Because good design helps to write good test I would recommend Practical Object-Oriented Design in Ruby: An Agile Primer by Sandi Metz(which is one of a best book I've read)