big refactor for test and admin namespace
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
require 'rails/generators'
|
||||
require 'rails/generators/generated_attribute'
|
||||
module BjController
|
||||
module Generators
|
||||
class SpecGenerator < ::Rails::Generators::Base
|
||||
source_root File.expand_path('../templates', __FILE__)
|
||||
argument :controller_name, :type => :string, :required => true
|
||||
|
||||
def copy_views
|
||||
generate_views
|
||||
end
|
||||
protected
|
||||
def generate_views
|
||||
template 'controller_spec.rb', File.join('spec/controllers', controller_namespace_path, "#{controller_base_name}_controller_spec.rb")
|
||||
end
|
||||
def controller_namespace_path
|
||||
controller_name.sub(/[^\/]+\Z/, '')
|
||||
end
|
||||
|
||||
def model_plural_name
|
||||
controller_base_name
|
||||
end
|
||||
def model_name
|
||||
model_plural_name.singularize
|
||||
end
|
||||
|
||||
def controller_base_name
|
||||
controller_name.split('/').last.pluralize
|
||||
end
|
||||
|
||||
def controller_class_name
|
||||
"#{controller_name.classify.pluralize}Controller"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,108 @@
|
||||
# encoding: UTF-8
|
||||
require 'spec_helper'
|
||||
|
||||
describe <%= controller_class_name %> do
|
||||
before :each do
|
||||
@administrator = Administrator.find_by_email('administrator@qwaiter.com') || Administrator.create(email: 'administrator@qwaiter.com', password: 'secret')
|
||||
sign_in @administrator
|
||||
end
|
||||
|
||||
describe "GET #index" do
|
||||
it "populates an array of <%= model_plural_name %>" do
|
||||
<%= model_name %> = create :<%= model_name %>
|
||||
get :index
|
||||
assigns(:<%= model_plural_name %>).should eq([<%= model_name %>])
|
||||
end
|
||||
|
||||
it "should render without errors when no objects are present" do
|
||||
get :index
|
||||
expect{ render_template :index }.not_to raise_error
|
||||
end
|
||||
|
||||
it "renders the :index view" do
|
||||
get :index
|
||||
response.should render_template :index
|
||||
end
|
||||
end
|
||||
|
||||
describe "GET #show" do
|
||||
it "assigns the requested <%= model_name %> to @<%= model_name %>" do
|
||||
<%= model_name %> = create :<%= model_name %>
|
||||
get :show, id: <%= model_name %>
|
||||
assigns(:<%= model_name %>).should eq(<%= model_name %>)
|
||||
end
|
||||
|
||||
it "renders the #show view" do
|
||||
<%= model_name %> = create :<%= model_name %>
|
||||
get :show, id: <%= model_name %>
|
||||
response.should render_template :show
|
||||
end
|
||||
end
|
||||
|
||||
describe "GET #new" do
|
||||
it "assigns a new <%= model_name %> to @<%= model_name %>" do
|
||||
get :new
|
||||
assigns(:<%= model_name %>).should be_a <%= model_name.classify %>
|
||||
end
|
||||
|
||||
it "renders the #show view" do
|
||||
get :new
|
||||
response.should render_template :new
|
||||
end
|
||||
end
|
||||
|
||||
describe "POST #create" do
|
||||
context "with valid attributes" do
|
||||
it "creates a new <%= model_name %>" do
|
||||
expect{
|
||||
post :create, <%= model_name %>: attributes_for(:<%= model_name %>)
|
||||
}.to change(<%= model_name.classify %>, :count).by(1)
|
||||
end
|
||||
|
||||
it "redirects to the new <%= model_name %>" do
|
||||
post :create, <%= model_name %>: attributes_for(:<%= model_name %>)
|
||||
response.should redirect_to <%= controller_namespace_path.present? ? "[#{controller_namespace_path.split('/').map{|n| ":#{n}"}.join(', ')}, #{model_name.classify}.last]" : "#{model_name.classify}.last" %>
|
||||
end
|
||||
end
|
||||
|
||||
context "with invalid attributes" do
|
||||
it "does not save the new <%= model_name %>" do
|
||||
expect{
|
||||
post :create, <%= model_name %>: {}
|
||||
}.to_not change(<%= model_name.classify %>, :count)
|
||||
end
|
||||
|
||||
it "re-renders the new method" do
|
||||
post :create, <%= model_name %>: {}
|
||||
response.should render_template :new
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'PUT update' do
|
||||
before :each do
|
||||
@<%= model_name %> = create :<%= model_name %>
|
||||
end
|
||||
|
||||
context "valid attributes" do
|
||||
it "located the requested <%= model_name %>" do
|
||||
put :update, id: @<%= model_name %>, <%= model_name %>: attributes_for(:<%= model_name %>)
|
||||
assigns(:<%= model_name %>).should eq(@<%= model_name %>)
|
||||
end
|
||||
|
||||
it "changes @<%= model_name %>'s attributes" do
|
||||
attributes = attributes_for(:<%= model_name %>)
|
||||
attribute_to_change = attributes.keys.find{|k| k !~ /_id$/}
|
||||
attributes[attribute_to_change] = "ChangedByTest"
|
||||
put :update, id: @<%= model_name %>, <%= model_name %>: attributes
|
||||
@<%= model_name %>.reload
|
||||
@<%= model_name %>.send(attribute_to_change).should eq("ChangedByTest")
|
||||
end
|
||||
|
||||
it "redirects to the updated <%= model_name %>" do
|
||||
put :update, id: @<%= model_name %>, <%= model_name %>: attributes_for(:<%= model_name %>)
|
||||
response.should redirect_to <%= controller_namespace_path.present? ? "[#{controller_namespace_path.split('/').map{|n| ":#{n}"}.join(', ')}, @#{model_name}]" : "@#{model_name}" %>
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user