From aac79592796b07251afd5d664484a96914e8cd66 Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Tue, 29 Nov 2011 09:08:33 +0900 Subject: [PATCH] tests for controller variable decoration --- spec/fake_app/authors/index.html.erb | 4 ++++ spec/fake_app/authors/show.html.erb | 2 ++ spec/fake_app/fake_app.rb | 29 +++++++++++++++++++++++++++ spec/requests/controller_ivar_spec.rb | 29 +++++++++++++++++++++++++++ 4 files changed, 64 insertions(+) create mode 100644 spec/fake_app/authors/index.html.erb create mode 100644 spec/fake_app/authors/show.html.erb create mode 100644 spec/requests/controller_ivar_spec.rb diff --git a/spec/fake_app/authors/index.html.erb b/spec/fake_app/authors/index.html.erb new file mode 100644 index 0000000..a228464 --- /dev/null +++ b/spec/fake_app/authors/index.html.erb @@ -0,0 +1,4 @@ +<% @authors.each do |author| %> + <%= author.name %> + <%= author.reverse_name %> +<% end %> diff --git a/spec/fake_app/authors/show.html.erb b/spec/fake_app/authors/show.html.erb new file mode 100644 index 0000000..761ec66 --- /dev/null +++ b/spec/fake_app/authors/show.html.erb @@ -0,0 +1,2 @@ +<%= @author.name %> +<%= @author.capitalized_name %> diff --git a/spec/fake_app/fake_app.rb b/spec/fake_app/fake_app.rb index 90589fb..3381da2 100644 --- a/spec/fake_app/fake_app.rb +++ b/spec/fake_app/fake_app.rb @@ -29,6 +29,35 @@ class Book < ActiveRecord::Base # helpers module ApplicationHelper; end +# decorators +module AuthorDecorator + def reverse_name + name.reverse + end + + def capitalized_name + name.capitalize + end +end + +# controllers +class ApplicationController < ActionController::Base + self.append_view_path File.dirname(__FILE__) +end +class AuthorsController < ApplicationController + def index + if params[:variable_type] == 'array' + @authors = Author.all + else + @authors = Author.scoped + end + end + + def show + @author = Author.find params[:id] + end +end + # migrations class CreateAllTables < ActiveRecord::Migration def self.up diff --git a/spec/requests/controller_ivar_spec.rb b/spec/requests/controller_ivar_spec.rb new file mode 100644 index 0000000..bb88a95 --- /dev/null +++ b/spec/requests/controller_ivar_spec.rb @@ -0,0 +1,29 @@ +require 'spec_helper' + +feature 'decorating controller ivar' do + background do + @matz = Author.create! :name => 'matz' + Author.create! :name => 'takahashim' + end + after do + Author.delete_all + end + + scenario 'decorating a model object in ivar' do + visit "/authors/#{@matz.id}" + page.should have_content 'matz' + page.should have_content 'matz'.capitalize + end + + scenario 'decorating model scope in ivar' do + visit '/authors' + page.should have_content 'takahashim' + page.should have_content 'takahashim'.reverse + end + + scenario "decorating models' array in ivar" do + visit '/authors?variable_type=array' + page.should have_content 'takahashim' + page.should have_content 'takahashim'.reverse + end +end