Rails 3, too many ways to test?

喜欢而已 提交于 2019-12-23 16:53:40

问题


If you were just starting out in rails which path would you encourage new users to go down with regards to Testing.

Anything I read about regarding Rails 3, tells me I should be using Rspec 2. But Rspec comes with a whole whack of other things I need to learn like

gem 'capybara'
gem 'database_cleaner'
gem 'cucumber-rails'
gem 'cucumber'
gem 'spork'
gem 'launchy'

And then one of my friends just told me that I should just stick to the testing framework that comes with rails, and maybe just integrate 'shoulda' into the test process because it has a nice syntax.

When it comes to testing it seems like we have way too many options and too many ways of doing something. Maybe this is a good thing, but I always thought rails was about creating common conventions and avoiding doing the same thing in different ways.

I'm fairly new to rails.
I'm unsure where to start.
I feel overwhelmed.
Is this normal?


回答1:


Since you're beginning with rails, I'd recommend using something that "just works" to get in the habit of testing your code and understanding how to work with your framework. Don't waste your time comparing solutions, choose one and stick with it for this project.

That being said, I really think it depends on your experience developing software:

  • if you've never used unit tests for your code, learn Test::Unit and consider adopting TDD. Notice that TDD will need you to dedicate a large amount of time to it at first, but that is paid off when you get used to it.
  • if you have significant experience with code tests and are just confused on how to start with rails, I recommend following through http://railstutorial.org/book. This is a (free) Ruby on Rails book that uses RSpec, Spork and serves as a great introduction

After developing one project from start to end using the testing platform you choose now, you'll have a much better task of choosing the testing framework you wish to use.

I'd also recommend you learn RSpec well before diving into Cucumber.




回答2:


Sounds like you have option overload. Which is common to most people in a lot of different situations even outside of programming. The absolute easiest thing for you to do, would be to go with what Rails sets up by default, which is called Test::Unit

Their official guide introduces it here: http://guides.rubyonrails.org/testing.html

Once you are comfortable with testing using plain Test::Unit style then I would suggest you explore some of the addons your friend mentioned such as shoulda (https://github.com/thoughtbot/shoulda), and factory_girl (https://github.com/thoughtbot/factory_girl)

Good luck!




回答3:


But Rspec comes with a whole whack of other things I need to learn like ...

Actually, you don't need to learn any of those other gems to get started with RSpec. Just go with gem 'rspec-rails, and run rails generate rspec:install to set up your project.

A good way to learn RSpec is to look at the code generated by rails generate scaffold. This creates spec files for controllers, views and routes that deal with the standard RESTful actions.

While learning RSpec you can add in other gems as you discover the need for them. Autotest is essential IMHO. Webrat adds useful matchers for view specs. Factory_girl adds support for factories.

Spork is great, but not really necessary until you have enough specs that performance is an issue.

I feel overwhelmed. Is this normal?

Less of this, more of this :)



来源:https://stackoverflow.com/questions/4844220/rails-3-too-many-ways-to-test

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!