RailsApps/rails3-devise-rspec-cucumber Better Specs RSpec Best Practices Click here to view the complete list of tools reviews This article was originally published in the Spring 2011 issue of Methods & Tools RSpec Best Practices Jared Carroll, Carbon Five, RSpec is a Behavior-Driven Development tool for Ruby programmers. Web Site: tested: 2.5License & Pricing: MIT License, open source / freeSupport: Community RSpec is a great tool in the behavior driven design process of writing human readable specifictions that direct and validate the development of your application. First #describe What You Are Doing Begin by using #describe for each of the methods you plan on defining, passing the method’s name as the argument. describe User do describe '.authenticate' do end describe '.admins' do end describe '#admin?' Then Establish The #context Next use #context to explain the different scenarios in which the method could be executed. For example, the following method has two execution paths: Lose The Should Before:
#275 How I Test Here I show how I would add tests to the password reset feature created in the previous episode. I use RSpec, Capybara, Factory Girl, and Guard to make request, model, and mailer specs. Download: source codeProject Files in Zip (92.2 KB)mp4Full Size H.264 Video (26.4 MB)m4vSmaller H.264 Video (16.1 MB)webmFull Size VP8 Video (18.1 MB)ogvFull Size Theora Video (37.8 MB) How I Test In a recent discussion on Google+, a friend of mine commented, "Test-Driven Development (TDD) and Behavior-Driven Development (BDD) is Ivory Tower BS." This prompted me to think about my first project, how I felt the same way then, and how I feel about it now. Since that first project, I've developed a rhythm of TDD/BDD that not only works for me, but for the client as well. Ruby on Rails ships with a test suite, called Test Unit, but many developers prefer to use RSpec, Cucumber, or some combination of the two. RSpec From the RSpec site: RSpec is a testing tool for the Ruby programming language. RSpec provides a powerful DSL that is useful for both unit and integration testing. Cucumber I've found the benefits of TDD/BDD far outweigh the cons. Cucumber is an integration and acceptance testing framework that supports Ruby, Java, .NET, Flex, and a host of other web languages and frameworks. The Setup Let's first begin a new project, instructing Rails to skip Test Unit. Our First Feature
RSpec | PeepCode Screencasts ▶ We’ve been acquired by Pluralsight! ▶ Upgrade your account to Pluralsight ▶ More details… Filter by tag: Ruby on Rails · Ruby · Javascript · Play by Play · Unix · RSpec · Deployment · Pdf · Design · Cocoa · iPhone · Node · Databases · Productivity · Git RSpec 2: with Rails 4 and Ruby 2 (Part 3 of 3) RSpec 2: Tools (Part 2 of 3) RSpec 2: The Basics (Part 1 of 3) Use the Cucumber RSpec Controllers and Tools RSpec Mocks and Models RSpec Basics Digesting Rails: BDD with RSpec | Robert Lysik I’ve now progressed through the first six chapters of the Ruby on Rails Tutorial and my head is spinning. I feel like Keanu Reeve’s character from the 90′s b-movie Johnny Mnemonic. Before I proceed, I thought I’d take some time to digest what I’ve learned and do a little research into aspects that I’ve found somewhat inscrutable, which are legion. Where to start? I’m somewhat intrigued by the idea of Test Driven Development, or in the case of the Ruby on Rails tutorial its variant which is described as Behavior Driven Development. Behavior Driven Development Behavior Driven Development or BDD is a framework for unit testing of software that seeks to rephrase test cases using more natural language. RSpec RSpec is a Behavior Driven Development tool for Ruby inspired by the work of Dan North and JBehave. A Simple Example The Ruby on Rails Tutorial contains an excellent overview of BDD and RSpec and interweaves testing using RSpec throughout each chapter. So, what’s up with “it?” Voila!
Documentation for rspec-rails (2.12.2) rspec-rails is a testing framework for Rails 3.x and 4.x. Use rspec-rails 1.x for Rails 2.x. Installation Add rspec-rails to both the :development and :test groups in the Gemfile: group :development, :test do gem 'rspec-rails', '~> 2.0'end Download and install by running: bundle install Initialize the spec/ directory (where specs will reside) with: rails generate rspec:install To run your specs, use the rspec command: bundle exec rspec bundle exec rspec spec/models bundle exec rspec spec/controllers/accounts_controller_spec.rb Specs can also be run via rake spec, though this command may be slower to start than the rspec command. In Rails 4, you may want to create a binstub for the rspec command so it can be run via bin/rspec: bundle binstubs rspec-core Generators Once installed, RSpec will generate spec files instead of Test::Unit test files when commands like rails generate model and rails generate controller are used. You may also invoke invoke RSpec generators independently. Model Specs Upgrade note
Ruby for Newbies: Testing with Rspec Ruby is a one of the most popular languages used on the web. We're running a Session here on Nettuts+ that will introduce you to Ruby, as well as the great frameworks and tools that go along with Ruby development. In this episode, you’ll learn about testing your Ruby code with Rspec, one of the best testing libraries in the business. If you’ve read my recent tutorial on JasmineJS, you’ll probably notice several similarities in Rspec. It’s pretty easy to install Rspec. That easy. Now, let’s set up a small project. Here’s what your project directory should look like: We put the specifications (or specs) in a spec folder; we have one spec file for each class. (Have you met require_relative yet? You may not be familiar with the YAML module; YAML is a simple text database that we’ll use to store data. So, now that we’re all set up, let’s get cracking on some specs! Let’s start with the tests for the Book class. This is how we start: with a describe block. Moving on, Here’s our first test.
Ruby for Newbies: Testing Web Apps with Capybara and Cucumber Ruby is one of the most popular languages used on the web. We’re running a Session here on Nettuts+ that will introduce you to Ruby, as well as the great frameworks and tools that go along with Ruby development. In this episode, you'll learn about testing your Sinatra apps with Cucumber, Capybara, and Rspec. In the previous tutorial in this series, we looked at Rspec, and how you can do test-driven development with it. Prefer a Screencast? Press HD for the clearest picture. Step 1: Building the App We’re going to create an incredibly simple Sinatra app to test. What? Now, run bundle install in that directory. So, open a file called myapp.rb; here’s our super simple app; it just simulates a site that might let you sign up for a newsletter. If you’re not familiar with Sinatra, check out Dan Harper’s excellent sessions Singing with Sinatra; that’ll get you up and running with the basics in no time. Now, let’s build these templates. layout.erb index.erb form.erb thankyou.erb So, there’s our app.
TestFirst.org - The Home of Test-First Teaching Fiero on Rails and Test First Development Fiero is the feeling of ecstatic triumph gamers feel when they solve a problem after working at it for ages. The ultimate expression of fiero is when you feel giddy. When you are compelled to shout or raise the roof because you feel like a hero or heroine of smartness. I experience this in programming every so often. It is what keeps me returning to it, during the slow climb of learning all of the basics. The other languages I'm familiar with report errors, but the reports are often vague or misleading or talk about something else that's perhaps somewhat *near* the thing that is broken, but really you must hunt it down yourself. I think the magical combination of a good text editing program (in my case the wonderful, very aggressively helpful Textmate) and rspec can put you on the track to Fiero. Developer friends used to joke about the "generate all code" button, and Rails comes close to doing this -- at least it generates the framework. Rails is user friendly and developer friendly!
7 Patterns to Refactor Fat ActiveRecord Models When teams use Code Climate to improve the quality of their Rails applications, they learn to break the habit of allowing models to get fat. “Fat models” cause maintenance issues in large apps. Only incrementally better than cluttering controllers with domain logic, they usually represent a failure to apply the Single Responsibility Principle (SRP). Early on, SRP is easier to apply. As you add more intrinsic complexity (read: features!) You may be thinking: “But Rails makes it really hard to do OOP right!” I used to believe that too. Don’t Extract Mixins from Fat Models Let’s get this out of the way. “Any application with an app/concerns directory is concerning.” And I agree. Now on to the refactorings! 1. Value Objects are simple objects whose equality is dependent on their value rather than an identity. In Rails, Value Objects are great when you have an attribute or small group of attributes that have logic associated with them. The #worse_than? 2. 3. 4. 5. 6. 7. Wrapping Up P.S.
Ruby on Rails - proper way to refactor this (single table inheritance?) model