background preloader

Test First / Refactor

Facebook Twitter

Refactoring code smell. 3 basic refactoring tools for your command line. Nowadays, I don't use any fancy editor or IDE, but for many years I worked with Eclipse, and I loved all the Java code refactoring tools it offered, even though most people only use pretty simple refactorings. So I used to believe that I needed things like RubyMine if I wanted to have the best refactoring tools, now I know I don't. I currently spend most of my time editing Ruby code with VIM, doing a lot of refactoring, and I have had a very satisfying experience. The operations I usually do fall into 3 main categories/tools, I'm going to include a short description of each category followed by an example of its use. First, let's install the utilities: -> brew install mmv the_silver_searcher ack And now, a quick summary of the commands I use the most: Now let's give a more in-depth explanation.

Some people use grep, but I prefer ack because of three reasons: it is faster, easier and cleaner. A) ag ActiveRecord This is to search the Rails codebase for the ActiveRecord constant usage: Have fun! Red, Green, Refactor - The Tools For Success. Feb 72012 It’s easy to say “We’re agile” and “We use Behavior/Test Driven Development” and thus “we use the right tools to empower our developers!” But what are those tools? For me that discussion is entirely about the tool stack you choose, how that stack empowers you as a developer to do things right the first time. Luckily thanks to the ruby community as a whole we have a large number of high-quality choice to choose between. Generally when we talk about TDD and being agile, beyond the process of choosing features to deliver the maximum customer facing value, we refer back to the simplest possible workflow of “Red/Green/Refactor.”

Within that workflow you write tests that fail, make them pass (with the focus on getting that done without polishing), then refactoring, to polish where necessary. The subject of testing is a huge one with a large body of prose already written on the subject. I propose a slightly different day-to-day workflow that I see used to great success. Great! Rails In The Workplace - Refactoring tools for Rails. 6 Steps To Refactoring Rails (for Mere Mortals) Since December, Rails has undergone a fairly significant internal refactoring in quite a number of areas. While it was quite tricky at first, we mere mortals have started to hone a process for diving into a new area of the codebase and emerging some time later with a much improved area that does basically the same thing. Here's the approach we've adopted and advocate: First, refactoring needs to be refactoring, not revision.

By that I mean that while you are in the process of invasively improving some code, it is not the appropriate time to also change the functionality of that code. If you do both at the same time, it will be difficult to track down whether a bug in the code is the result of refactoring or functionality changes. We've held fast to this requirement for the Rails 3 work Carl and I have been doing, which has resulted in an extremely stable edge, despite making fairly invasive changes. Second, any kind of significant refactoring without tests is folly. So that's it. Tagged: Web Engineering Textbook: Refactoring Rails. 1 Never be ashamed... of making you code better. On the contrary: recognizing code smells in your old code means that you learnt something in the meantime. Only very inexperienced people think that the code they wrote yesterday is perfect. As we have learned previously refactoring is "restructuring an existing body of codealtering its internal structurewithout changing its external behavior" wikipedia We will use tests to ensure that we do not change the external behavior of the code we are refactoring. 1.1 Tools for Code Quality Recognizing code that is problematic and should be refactored is one of the main skills of a developer.

But there is a role for tools in this process: especially when faced with a lot of code there are tools that can help you find places you should look at. A code metric is a quantitive measure of the quality of a piece of code. The Gem metric_fu combines some metrics. Run rake metrics:all and the results will be saved as html-files in tmp/metric_fu/output/. 2 Tools. Rails Refactor is now a Gem! - James Crisp. #212 Refactoring & Dynamic Delegator. Learn how to refactor a set of conditional Active Record queries using a Dynamic Delegator. Download: source codeProject Files in Zip (102 KB)mp4Full Size H.264 Video (11.9 MB)m4vSmaller H.264 Video (8.19 MB)webmFull Size VP8 Video (21.1 MB)ogvFull Size Theora Video (17.1 MB) #12 Refactoring User Name Part 3. In the final part of this series you will see how to refactor your tests.

Keeping tests clean is important because it will make testing easier to do in the future. Download: mp4Full Size H.264 Video (7.1 MB)m4vSmaller H.264 Video (4.75 MB)webmFull Size VP8 Video (12.9 MB)ogvFull Size Theora Video (9.7 MB) #11 Refactoring User Name Part 2. Testing and refactoring go hand in hand. Refactoring is all about improving code without changing its behavior. Testing is all about making sure you don't change the behavior while you are improving the code.

Download: mp4Full Size H.264 Video (9.89 MB)m4vSmaller H.264 Video (6.8 MB)webmFull Size VP8 Video (18.9 MB)ogvFull Size Theora Video (13.1 MB) #10 Refactoring User Name Part 1. Learn how to clean up your code through refactoring. This episode will show you how to move code from the view into the model to remove duplication and simplify the view. Download: mp4Full Size H.264 Video (8.48 MB)m4vSmaller H.264 Video (5.75 MB)webmFull Size VP8 Video (16.1 MB)ogvFull Size Theora Video (9.37 MB)

#91 Refactoring Long Methods. #101 Refactoring Out Helper Object. If you have complex view logic, this can easily lead to helper methods which call each other. See how to refactor this out into another object in this episode. This is also known as the presenter pattern. Download: mp4Full Size H.264 Video (14.2 MB)m4vSmaller H.264 Video (8.65 MB)webmFull Size VP8 Video (22.3 MB)ogvFull Size Theora Video (20.2 MB)

Ruby on Rails - proper way to refactor this (single table inheritance?) model. 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! Rspec. 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. Actually, the similarities are in Jasmine: Jasmine was created with Rspec in mind. 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. Rails Tutorial · Devise with RSpec and Cucumber. Devise with RSpec and Cucumber Introduction Ruby on Rails tutorial showing how to create a Rails 3.2 application using Devise with RSpec and Cucumber. Devise provides ready-made authentication and user management. RSpec is a popular framework for unit testing. Cucumber is used for integration testing and behavior driven development. The combination of Devise, RSpec, and Cucumber is the foundation for many real-world Rails applications. Is It for You? This tutorial is for experienced Rails developers as well as startup founders or hobbyist coders who are new to Rails.

Experienced developers will find the complete application on GitHub; this tutorial provides the detail and background to understand the implementation in depth. For Rails beginners, this tutorial describes each step that you must follow to create the application. This is one in a series of Rails example apps and tutorials from the RailsApps Project. This example application uses ActiveRecord and a SQLite database. Assumptions Fork. 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. BDD is an approach to software development that combines Test-Driven Development, Domain Driven Design and Acceptance Test-Driven Planning.

RSpec helps you do the TDD part of that equation, focusing on the documentation and design aspects of TDD. 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. Then Establish The #context Lose The Should. RSpec and Rails. 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. I mentioned that, in the next episode, we’d look at using Rspec to test web apps. 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. layout.erb.

RailsApps/rails3-devise-rspec-cucumber. 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. While creating a basic Rails application today as outlined in , I found it. 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. Rails is user friendly and developer friendly! Code: This was just proofreading. 7 Patterns to Refactor Fat ActiveRecord Models. How I Test.