Permalink
Browse files

Specs

  • Loading branch information...
1 parent b128593 commit 2e551d51274de8dfc1963975c938a19d75884a6d @thegcat committed Aug 6, 2011
Showing with 104 additions and 0 deletions.
  1. +11 −0 init.rb
  2. +67 −0 spec/controllers/projects_controller_spec.rb
  3. +26 −0 spec/spec_helper.rb
View
11 init.rb
@@ -0,0 +1,11 @@
+require 'redmine'
+
+Redmine::Plugin.register :chiliproject_ensure_project_hierarchy do
+ name 'Ensure Project Hierarchy plugin'
+ author 'Felix Schäfer'
+ description 'This plugin ensures subproject identifiers are prefixes with their parent project\'s identifier.'
+ version '0.0.1'
+ url 'https://github.com/thegcat/chiliproject_ensure_project_hierarchy'
+
+ ::Project::IDENTIFIER_SEPARATOR = "-"
+end
View
67 spec/controllers/projects_controller_spec.rb
@@ -0,0 +1,67 @@
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+
+describe ProjectsController do
+ let(:project) {mock_model Project}
+ let(:parent_project) {mock_model Project, :identifier => "something-witty"}
+
+ describe "when I navigate to the new project page after having clicked the 'New subproject' link" do
+ let(:do_action) {get :new, :parent_id => parent_project}
+
+ before do
+ # not testing the other stuff
+ IssueCustomField.stub!(:find)
+ Tracker.stub!(:all)
+
+ Project.stub!(:new).and_return project
+ @controller.stub!(:authorize_global).and_return true
+ end
+
+ describe "then the identifier of the new project" do
+ it "should default to the parent's identifier with the project identifier separator" do
+ project.should_receive(:identifier=).with "#{parent_project.identifier}#{::Project::IDENTIFIER_SEPARATOR}"
+ do_action
+ end
+ end
+ end
+
+ describe "when I try to save a project with a parent project" do
+ describe "when the project to save is an existing project" do
+ let(:do_action) {put :create, :project_id => "abc", :project => {:name => "abc", :parent_id => parent_project.id, :identifier => identifier}}
+ let(:project) {mock_model Project, :name => "abc", :parent_id => parent_project.id, :identifier => identifier}
+
+ before do
+ project.stub!(:safe_attributes=)
+ project.stub!(:save).and_return true
+ project.stub!(:set_allowed_parent!)
+ @controller.stub!(:authorize).and_return true
+ end
+
+ describe "and the submitted identifier is correct" do
+ let(:identifier) {"#{parent_project.identifier}#{::Project::IDENTIFIER_SEPARATOR}"}
+
+ it "then the creation should be successful" do
+ do_action
+ response.should be_success
+ end
+ end
+
+ describe "and the submitted identifier is incorrect" do
+ let(:identifier) {""}
+ let(:errors) {mock ActiveRecord::Errors}
+
+ before do
+ project.stub!(:errors).and_return errors
+ end
+
+ it "then an error should be set on the project" do
+ errors.should_receive(:add).with :identifier, :prefix_invalid, :prefix => "#{parent_project.identifier}#{::Project::IDENTIFIER_SEPARATOR}"
+ do_action
+ end
+ end
+ end
+
+ describe "when the project to save is a new project" do
+ # this uses the same validation, no need to test it again
+ end
+ end
+end
View
26 spec/spec_helper.rb
@@ -0,0 +1,26 @@
+RAILS_ENV = "test" unless defined? RAILS_ENV
+
+# prevent case where we are using rubygems and test-unit 2.x is installed
+begin
+ require 'rubygems'
+ gem "test-unit", "~> 1.2.3"
+rescue LoadError
+end
+
+begin
+ #require "config/environment" unless defined? RAILS_ROOT
+ require 'spec/spec_helper'
+rescue LoadError => error
+ puts <<-EOS
+
+ You need to install rspec in your ChiliProject project.
+ Please execute the following code:
+
+ gem install rspec-rails
+ script/generate rspec
+
+ EOS
+ raise error
+end
+
+require 'redmine_factory_girl'

0 comments on commit 2e551d5

Please sign in to comment.