Base
# File lib/generators/rspec/scaffold/scaffold_generator.rb, line 22 def generate_controller_spec return unless options[:controller_specs] template 'controller_spec.rb', File.join('spec/controllers', controller_class_path, "#{controller_file_name}_controller_spec.rb") end
# File lib/generators/rspec/scaffold/scaffold_generator.rb, line 43 def generate_routing_spec return unless options[:routing_specs] template 'routing_spec.rb', File.join('spec/routing', controller_class_path, "#{controller_file_name}_routing_spec.rb") end
# File lib/generators/rspec/scaffold/scaffold_generator.rb, line 59 def copy_view(view) template "#{view}_spec.rb", File.join("spec/views", controller_file_path, "#{view}.html.#{options[:template_engine]}_spec.rb") end
Returns the name of the mock. For example, if the file name is user, it returns mock_user.
If a hash is given, it uses the hash key as the ORM method and the value as response. So, for ActiveRecord and file name “User”:
mock_file_name(:save => true) #=> mock_user(:save => true)
If another ORM is being used and another method instead of save is called, it will be the one used.
# File lib/generators/rspec/scaffold/scaffold_generator.rb, line 98 def mock_file_name(hash=nil) if hash method, and_return = hash.to_a.first method = orm_instance.send(method).split('.').last.gsub(/\(.*?\)/, '') "mock_#{ns_file_name}(:#{method} => #{and_return})" else "mock_#{ns_file_name}" end end
support for namespaced-resources
# File lib/generators/rspec/scaffold/scaffold_generator.rb, line 69 def ns_file_name if ARGV[0].match(/(\w+)\/(\w+)/) "#{$1.underscore}_#{$2.singularize.underscore}" else file_name end end
support for namespaced-resources
# File lib/generators/rspec/scaffold/scaffold_generator.rb, line 78 def ns_table_name if ARGV[0].match(/(\w+)\/(\w+)/) "#{$1.underscore}/#{$2.tableize}" else table_name end end
# File lib/generators/rspec/scaffold/scaffold_generator.rb, line 64 def params "{'these' => 'params'}" end
Receives the ORM chain and convert to expects. For ActiveRecord:
should! orm_class.find(User, "37") #=> User.should_receive(:find).with(37)
For Datamapper:
should! orm_class.find(User, "37") #=> User.should_receive(:get).with(37)
# File lib/generators/rspec/scaffold/scaffold_generator.rb, line 118 def should_receive(chain) stub_or_should_chain(:should_receive, chain) end
Receives the ORM chain and convert to stub. For ActiveRecord:
stub orm_class.find(User, "37") #=> User.stub(:find).with(37)
For Datamapper:
stub orm_class.find(User, "37") #=> User.stub(:get).with(37)
# File lib/generators/rspec/scaffold/scaffold_generator.rb, line 132 def stub(chain) stub_or_should_chain(:stub, chain) end
# File lib/generators/rspec/scaffold/scaffold_generator.rb, line 136 def stub_or_should_chain(mode, chain) receiver, method = chain.split(".") method.gsub!(/\((.*?)\)/, '') response = "#{receiver}.#{mode}(:#{method})" response << ".with(#{$1})" unless $1.blank? response end
Generated with the Darkfish Rdoc Generator 2.