Permalink
Browse files

cleanup this spec-mess, use Rails.env / Rails.root

  • Loading branch information...
1 parent 8284a01 commit 510fe9b44ae34dfc6aa5970b2f7da1f685bb9df0 @grosser committed Mar 27, 2010
Showing with 107 additions and 139 deletions.
  1. +2 −11 Rakefile.rb
  2. +1 −3 init.rb
  3. +1 −1 lib/valid_attributes.rb
  4. +12 −7 spec/setup_test_model.rb
  5. +7 −36 spec/spec_helper.rb
  6. +84 −81 spec/valid_attributes_spec.rb
View
@@ -1,12 +1,3 @@
-require 'rubygems'
-require 'spec'
-
-desc 'Default: run spec.'
task :default => :spec
-
-desc "Run all specs in spec directory"
-task :spec do |t|
- options = "--colour --format progress --loadby --reverse"
- files = FileList['spec/**/*_spec.rb']
- system("spec #{options} #{files}")
-end
+require 'spec/rake/spectask'
+Spec::Rake::SpecTask.new {|t| t.spec_opts = ['--color']}
View
@@ -1,3 +1 @@
-if RAILS_ENV == 'test'
- require File.join(File.dirname(__FILE__),'lib','valid_attributes')
-end
+require 'valid_attributes' if Rails.env == 'test'
View
@@ -57,7 +57,7 @@ def self.valid_attributes(record_name)
def self.path_to_valid_yml
%w[test spec].each do |folder|
- file = File.join(RAILS_ROOT,folder,'valid_attributes.yml')
+ file = File.join(Rails.root, folder, 'valid_attributes.yml')
return file if File.exist?(file)
end
raise "create valid_attributes.yml in test/ or spec/"
View
@@ -1,17 +1,22 @@
-require 'rubygems'
require 'active_record'
-#create model table
+# connect
+ActiveRecord::Base.establish_connection(
+ :adapter => "sqlite3",
+ :database => ":memory:"
+)
+
+# table
ActiveRecord::Schema.define(:version => 1) do
- create_table "users" do |t|
- t.string "name"
- t.integer "age"
- t.boolean "ugly"
+ create_table :users do |t|
+ t.string :name
+ t.integer :age
+ t.boolean :ugly
t.timestamps
end
end
-#create model
+# model
class User < ActiveRecord::Base
validates_presence_of :name
attr_accessible :name, :age
View
@@ -1,42 +1,13 @@
-# ---- requirements
require 'rubygems'
require 'spec'
-require 'mocha'
+require 'spec/setup_test_model'
-$LOAD_PATH << File.expand_path("../lib", File.dirname(__FILE__))
+$LOAD_PATH << 'lib'
-
-# ---- rspec
-Spec::Runner.configure do |config|
- config.mock_with :mocha
-end
-
-
-# ---- bugfix
-#`exit?': undefined method `run?' for Test::Unit:Module (NoMethodError)
-#can be solved with require test/unit but this will result in extra test-output
-module Test
- module Unit
- def self.run?
- true
- end
+# fake rails being present
+class Rails
+ def self.env
+ 'test'
end
end
-
-# ---- load active record
-require 'active_record'
-
-
-RAILS_ENV = "test"
-ActiveRecord::Base.configurations = {"test" => {
- :adapter => "sqlite3",
- :database => ":memory:",
-}.with_indifferent_access}
-
-ActiveRecord::Base.logger = Logger.new(File.directory?("log") ? "log/#{RAILS_ENV}.log" : "/dev/null")
-ActiveRecord::Base.establish_connection(:test)
-
-
-# ---- setup environment/plugin
-require File.expand_path("../init", File.dirname(__FILE__))
-load File.expand_path("setup_test_model.rb", File.dirname(__FILE__))
+require 'init'
@@ -3,102 +3,105 @@
VALID_ATTRIBUTES_YML = File.join(File.dirname(__FILE__),'valid_attributes.yml')
ValidAttributesHelper.stored_valid_attributes = YAML.load(IO.read(VALID_ATTRIBUTES_YML))
-include ValidAttributes
-
-def preserving_rails_root
- temp = defined?(RAILS_ROOT) ? RAILS_ROOT : nil
- yield
- const_set('RAILS_ROOT',temp) if temp
-end
-
-describe :path_to_valid_yml do
- it "builds depending on RAILS_ROOT" do
- preserving_rails_root do
- RAILS_ROOT='xxx'
+describe ValidAttributes do
+ include ValidAttributes
+
+ describe :path_to_valid_yml do
+ it "builds depending on RAILS_ROOT" do
+ Rails.stub!(:root).and_return 'xxx'
+ File.stub!(:exist?).and_return true
ValidAttributesHelper.path_to_valid_yml.should == 'xxx/test/valid_attributes.yml'
end
end
-end
-describe :valid_attributes do
- it "loads from valid_attributes.yml" do
- ValidAttributesHelper.stored_valid_attributes = nil
- ValidAttributesHelper.expects(:path_to_valid_yml).returns VALID_ATTRIBUTES_YML
- ValidAttributesHelper.valid_attributes(User)[:name].should == 'Hans'
- end
- it "uses the given stored attributes" do
- valid_attributes(:user)[:name].should == 'Hans'
- end
- it "can use String and Symbol" do
- valid_attributes('user')[:name].should == 'Hans'
- valid_attributes(User)[:name].should == 'Hans'
- end
- it "merges the given attributes" do
- valid_attributes(User,:name=>'Peter')[:name].should == 'Peter'
- end
-end
+ describe :valid_attributes do
+ it "loads from valid_attributes.yml" do
+ ValidAttributesHelper.stored_valid_attributes = nil
+ ValidAttributesHelper.should_receive(:path_to_valid_yml).and_return VALID_ATTRIBUTES_YML
+ ValidAttributesHelper.valid_attributes(User)[:name].should == 'Hans'
+ end
-describe :valid do
- it "creates a new record with all(even not-accessible) attributes" do
- user = valid User
- valid_attributes(User).each do |field,value|
- user.send(field).should == value
+ it "uses the given stored attributes" do
+ valid_attributes(:user)[:name].should == 'Hans'
end
- end
- it "can use String or Symbol" do
- valid('user').attributes.should == valid(User).attributes
- valid(:user).attributes.should == valid(User).attributes
- end
- it "adds given set of attributes(even not-accessible)" do
- changed = {:age=>valid_attributes(User)[:age] + 10,:ugly=>!valid_attributes(User)[:ugly]}
- user = valid User, changed
- changed.each do |field,value|
- user.send(field).should == value
+
+ it "can use String and Symbol" do
+ valid_attributes('user')[:name].should == 'Hans'
+ valid_attributes(User)[:name].should == 'Hans'
end
- end
-end
-describe :create_valid do
- it "creates a valid record" do
- user = create_valid User
- user.name.should == 'Hans'
- user.should_not be_new_record
- end
- it "passes values to valid" do
- expects(:valid).with(User,:age=>123).returns User.new(:name=>'Peter')
- create_valid(User,:age=>123)
+ it "merges the given attributes" do
+ valid_attributes(User,:name=>'Peter')[:name].should == 'Peter'
+ end
end
-end
-describe :assert_invalid_value do
- class VATestException < Exception
- end
+ describe :valid do
+ it "creates a new record with all(even not-accessible) attributes" do
+ user = valid User
+ valid_attributes(User).each do |field,value|
+ user.send(field).should == value
+ end
+ end
- def assert_block(text)
- raise VATestException.new("failed") unless yield
- end
-
- it "passes for single invalid value" do
- assert_invalid_value(User,:name,nil)
- end
- it "passes for multiple invalid values" do
- assert_invalid_value(User,:name,[nil,''])
+ it "can use String or Symbol" do
+ valid('user').attributes.should == valid(User).attributes
+ valid(:user).attributes.should == valid(User).attributes
+ end
+
+ it "adds given set of attributes(even not-accessible)" do
+ changed = {:age=>valid_attributes(User)[:age] + 10,:ugly=>!valid_attributes(User)[:ugly]}
+ user = valid User, changed
+ changed.each do |field,value|
+ user.send(field).should == value
+ end
+ end
end
- it "fails for multiple valid value" do
- lambda{assert_invalid_value(User,:name,[nil,'Hans'])}.should raise_error(VATestException)
+
+ describe :create_valid do
+ it "creates a valid record" do
+ user = create_valid User
+ user.name.should == 'Hans'
+ user.should_not be_new_record
+ end
+
+ it "passes values to valid" do
+ should_receive(:valid).with(User,:age=>123).and_return User.new(:name=>'Peter')
+ create_valid(User,:age=>123)
+ end
end
- it "builds a readable failure message" do
- expects(:assert_block).with do |text|
- text == '<User.name> expected to be invalid when set to <>'
+
+ describe :assert_invalid_value do
+ class VATestException < Exception
+ end
+
+ def assert_block(text)
+ raise VATestException.new("failed") unless yield
+ end
+
+ it "passes for single invalid value" do
+ assert_invalid_value(User,:name,nil)
+ end
+
+ it "passes for multiple invalid values" do
+ assert_invalid_value(User,:name,[nil,''])
+ end
+ it "fails for multiple valid value" do
+ lambda{assert_invalid_value(User,:name,[nil,'Hans'])}.should raise_error(VATestException)
+ end
+
+ it "builds a readable failure message" do
+ should_receive(:assert_block).with do |text|
+ text == '<User.name> expected to be invalid when set to <>'
+ end
+ assert_invalid_value(User,:name,nil)
end
- assert_invalid_value(User,:name,nil)
end
-end
-describe :assert_invalid_attributes do
- it "calls assert_invalid_value for each set" do
- expects(:assert_invalid_value).with(User,:name,['Hans',nil])
- expects(:assert_invalid_value).with(User,:age,11)
- assert_invalid_attributes(User,:name=>['Hans',nil],:age=>11)
+ describe :assert_invalid_attributes do
+ it "calls assert_invalid_value for each set" do
+ should_receive(:assert_invalid_value).with(User,:name,['Hans',nil])
+ should_receive(:assert_invalid_value).with(User,:age,11)
+ assert_invalid_attributes(User,:name=>['Hans',nil],:age=>11)
+ end
end
end

0 comments on commit 510fe9b

Please sign in to comment.