Home arrow Ruby-on-Rails arrow Page 2 - Preparing to Add Tagging Support
RUBY-ON-RAILS

Preparing to Add Tagging Support


In this second part of a four-part series on how to add tagging support to an ecommerce website using Ruby on Rails, you'll learn what modifications you need to make so that items in your system can be tagged without breaking things. This article is excerpted from chapter seven of the book Practical Rails Projects, written by Eldon Alameda (Apress; ISBN: 1590597818).

Author Info:
By: Apress Publishing
Rating: 5 stars5 stars5 stars5 stars5 stars / 1
June 09, 2010
TABLE OF CONTENTS:
  1. · Preparing to Add Tagging Support
  2. · Unit Testing the Model
  3. · Using the Console to Test the Model

print this article
SEARCH DEVARTICLES

Preparing to Add Tagging Support - Unit Testing the Model
(Page 2 of 3 )

Upgrading the acts_as_taggable gem or changing your own code can easily break the tagging functionality. To prevent this, or at least minimize the risk of this happening in our production environment, we’ll create unit tests for the model. These tests will be used to verify that we can add tags to a book and find the book.

Opentest/unit/book_test.rbin your editor and add the followingtest_taggingmethod:

require File.dirname(__FILE__) + '/../test_helper'

class BookTest < Test::Unit::TestCase
  fixtures :publishers, :authors, :books, :authors_books

  def test_tagging
    book = Book.find(1)
    book.tag 'Elvis,Thriller', :separator => ','

    book.reload

    assert book.tagged_with?('Elvis')
   
assert book.tagged_with?('Thriller')

    assert_equal 2, book.tags.size
   
assert_equal ['Elvis', 'Thriller'], book.tag_names

    assert_equal 1, Book.find_tagged_with(:any => [ 'Elvis', 'Thriller' ]).size
    assert_equal 1, Book.find_tagged_with(:all => [ 'Elvis', 'Thriller' ]).size
  end
  def test_failing_create

The unit test first loads a single book from thebooksfixture and assigns the tagsElvisandThrillerto it. Then the test reloads the book from the database and verifies that the book has been tagged correctly. The verification is done by using theacts_as_taggableAPI methodstagged_withandfind_tagged_with.


Tip  You can usebook.errors.full_messages.join(';')to get a string representation of all validation errors after an unsuccessful save of an ActiveRecord object. This is useful when debugging validations.


Next, run the unit test, and you should see all tests pass:

$ ruby test/unit/book_test.rb

--------------------------------------------
Loaded suite test/unit/book_test Started
.....
Finished in 0.159311 seconds.

5 tests, 22 assertions, 0 failures, 0 errors
--------------------------------------------


blog comments powered by Disqus
RUBY-ON-RAILS ARTICLES

- Ruby-on-Rails Faces Second Security Flaw in ...
- Ruby 2.0 Prepped for February 2013 Release
- Why LinkedIn Switched from Ruby on Rails
- Adding Style with Action Pack
- Handling HTML in Templates with Action Pack
- Filters, Controllers and Helpers in Action P...
- Action Pack and Controller Filters
- Action Pack Categories and Events
- Logging Out, Events and Templates with Actio...
- Action Pack Sessions and Architecture
- More on Action Pack Partial Templates
- Action Pack Partial Templates
- Displaying Error Messages with the Action Pa...
- Action Pack Request Parameters
- Creating an Action Pack Registration Form

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials