开发者

Looking for Examples of Rails 3 Integration/Acceptance tests with RSpec [closed]

开发者 https://www.devze.com 2023-03-13 15:50 出处:网络
Closed. This question is seeking recommendations for books, tools, software libraries, and more. It does not meet Stack Overflow guidelines guidelines. It is not currently accepting answers.
Closed. This question is seeking recommendations for books, tools, software libraries, and more. It does not meet Stack Overflow guidelines guidelines. It is not currently accepting answers.

We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.

Closed 8开发者_如何学Python years ago.

Improve this question

As a long time php dev and a rails newb I've been trying to get a grasp on tdd/bdd. There are so many tools and methodologies out there that this can be a pretty daunting task. After a lot of reading I came to the conclusion that this would be my preferred method of testing:

  • No cucumber, it seems to me that it's just an unnecessary complexity.
  • RSpec for unit tests
  • RSpec/Capybara for integration/acceptance tests.
  • No testing for controllers/views.
  • Factory Girl or Machinist for factories.
  • Don't mock my own code.

Being a total BDD newb, this could be way off base so feel free to offer suggestions on this testing 'stack'.

What I'm really looking for though is an open source project using these tools that I can look to for inspiration.

Tutorials would be appreciated too, but I think seeing a real world project implement all these ideas would be very helpful.


Everyone seems to have their own recipe for testing that works for them. My background is like yours -- long-time PHP developer using no tests (but doing plenty of manual white-box testing while developing). I recently started writing my first production-level, getting-paid-for-playing-with-Rails app. Here are my thoughts after the first three months.

I too found Cucumber to be too complex for me to start with. I struggled with it for far too long before finally realizing that I was just too far removed from the actual testing framework. Once I dropped down to pure RSpec things became much clearer. That said, now that I've written my first app with a robust test suite I think I could probably drop Cucumber into my next Rails app recipe and feel more comfortable using it for integration testing while still using RSpec for unit testing.

I've developed an unhealthy infatuation with RSpec once I finally understood the process. It really is beautiful to run those tests and see all green. Actually, it's even satisfying to see red because it tells me what to work on next.

I'm using the default Webrat for integration tests. I haven't run up against anything that Webrat couldn't solve, but I haven't given Capybara a try either so maybe I just don't know how bad I have it.

I started writing my tests by following along with the Rails Tutorial Book, and in it the author says "Since I’m not partial to separate tests for views or helpers, which I’ve found to be either brittle or redundant, our first step is to remove them." (Page 93, paragraph 2) After writing a bunch of helper methods, I decided to go back and write separate helper tests for all of them. I'm still rolling my view tests into my controller tests, but I could see separating them in my next project. FWIW, I think you really should be writing tests for everything at some level, even if you're just relying on your controller tests to exercise your views, and/or your view tests to exercise your helper methods.

I'm using Factory Girl with this project and I have found it to be very useful and easy to use. I haven't tried Machinist but it seems to be equally popular. One thing I will mention is that when I first started writing my factories I also used the FFaker gem to generate random content into every factory object that I created. This seemed nice at first as the variable content sometimes caused me to find things that I had missed in development (can't think of a good example at the moment, though), but it also meant I had to worry about truncating all of the returned FFaker data to fit wihtin the length constraints in the model. This proved to be too unwieldy in the long run so I removed all of that code and went with using static strings for all my factories, and only for the required fields. Then when I wanted to exercise a specific field I would do so in my actual specs. IMO, your factories should contain only enough data to create a valid object and no more.

As for mocking, I've only found a few places where I needed to mock something, but perhaps I'm too new to Rspec to know that I'm doing it wrong.

And finally, as for specific examples, just look on GitHub for example Rails apps or open source Rails projects that contain tests. Or follow along with the Rails Tutorial Book as I did. Whatever you decide to do, start as close to the metal as possible and then try different gems/plugins/etc, always making sure you can back off your changes if you decide you don't like the results. I found that trying to start with a complete recipe (i.e. suspenders, blue-light-special, etc.) was too overwhelming. By starting with a raw Rails app I was able to try out different solutions (paperclip versus carrierwave, typus versus rails_admin, clearance versus devise, etc.) to see which ones worked for my personal preferences. It's clear that there is no one perfect recipe for a Rails app, and everyone's preferred recipe is just an opinion.

0

精彩评论

暂无评论...
验证码 换一张
取 消