Permalink
Browse files

fix test pollution when lets are used in before(:all)

Change-Id: I6d09877aba43b916045d41625f951cb26c54bae3
  • Loading branch information...
1 parent ffa9082 commit 69f32c130c33744da066cb1b78d298637867539e @kowshik kowshik committed Jan 23, 2013
@@ -4,15 +4,15 @@
module VCAP::CloudController
describe VCAP::CloudController::AppSummary do
- let(:num_services) { 2 }
- let(:free_mem_size) { 128 }
-
let(:admin_headers) do
user = VCAP::CloudController::Models::User.make(:admin => true)
headers_for(user)
end
- before :all do
+ before(:all) do
+ @num_services = 2
+ @free_mem_size = 128
+
@system_domain = Models::Domain.new(:name => Sham.domain,
:owning_organization => nil)
@system_domain.save(:validate => false)
@@ -26,11 +26,11 @@ module VCAP::CloudController
:space => @space,
:production => false,
:instances => 1,
- :memory => free_mem_size,
+ :memory => @free_mem_size,
:state => "STARTED",
)
- num_services.times do
+ @num_services.times do
instance = Models::ServiceInstance.make(:space => @space)
@services << instance
Models::ServiceBinding.make(:app => @app, :service_instance => instance)
@@ -110,7 +110,7 @@ module VCAP::CloudController
end
it "should return num_services services" do
- decoded_response["services"].size.should == num_services
+ decoded_response["services"].size.should == @num_services
end
it "should return the correct info for a service" do
@@ -18,14 +18,27 @@ def query_params_for_inline_depth(depth)
end
shared_context "collections" do |opts, attr, make|
- let(:obj) { opts[:model].make }
- let(:other_obj) { opts[:model].make }
- let(:add_method) { "add_#{child_name}" }
- let(:get_method) { "#{child_name}s" }
- let(:child_name) { attr.to_s.singularize }
- let(:headers) do
- user = VCAP::CloudController::Models::User.make(:admin => true)
- json_headers(headers_for(user))
+ define_method(:obj) do
+ @obj ||= opts[:model].make
+ end
+
+ define_method(:child_name) do
+ attr.to_s.singularize
+ end
+
+ def add_method
+ "add_#{child_name}"
+ end
+
+ def get_method
+ "#{child_name}s"
+ end
+
+ def headers
+ @header ||= begin
+ user = VCAP::CloudController::Models::User.make(:admin => true)
+ json_headers(headers_for(user))
+ end
end
before(:all) do
@@ -1,33 +1,34 @@
# Copyright (c) 2009-2012 VMware, Inc.
module VCAP::CloudController::ApiSpecHelper
-
shared_examples "creating and updating" do |opts|
describe "creating and updating" do
- let(:creation_opts) do
- # if the caller has supplied their own creation lambda, use it
- opts[:create_attribute_reset].call if opts[:create_attribute_reset]
-
- initial_obj = opts[:model].make
- attrs = creation_opts_from_obj(initial_obj, opts)
- initial_obj.destroy
-
- create_attribute = opts[:create_attribute]
- if create_attribute
- opts[:create_attribute_reset].call
- attrs.keys.each do |k|
- v = create_attribute.call k
- attrs[k] = v if v
+ define_method(:creation_opts) do
+ @creation_opts ||= begin
+ # if the caller has supplied their own creation lambda, use it
+ opts[:create_attribute_reset].call if opts[:create_attribute_reset]
+
+ initial_obj = opts[:model].make
+ attrs = creation_opts_from_obj(initial_obj, opts)
+ initial_obj.destroy
+
+ create_attribute = opts[:create_attribute]
+ if create_attribute
+ opts[:create_attribute_reset].call
+ attrs.keys.each do |k|
+ v = create_attribute.call k
+ attrs[k] = v if v
+ end
end
- end
- opts[:extra_attributes].each do |attr|
- if opts[:required_attributes].include?(attr)
- attrs[attr.to_s] = Sham.send(attr)
+ opts[:extra_attributes].each do |attr|
+ if opts[:required_attributes].include?(attr)
+ attrs[attr.to_s] = Sham.send(attr)
+ end
end
- end
- attrs
+ attrs
+ end
end
let(:non_synthetic_creation_opts) do
@@ -118,9 +119,11 @@ module VCAP::CloudController::ApiSpecHelper
req_attrs = req_attrs - ["id"] if verb == :put
req_attrs.each do |without_attr|
context "without the :#{without_attr.to_s} attribute" do
- let(:filtered_opts) do
- creation_opts.select do |k, v|
- k != without_attr.to_s and k != "#{without_attr}_id"
+ define_method(:filtered_opts) do
+ @filtered_opts ||= begin
+ creation_opts.select do |k, v|
+ k != without_attr.to_s and k != "#{without_attr}_id"
+ end
end
end
@@ -246,7 +249,6 @@ module VCAP::CloudController::ApiSpecHelper
it_behaves_like "a vcap rest error response", /taken/
end
end
-
end
end
end
@@ -4,10 +4,9 @@ module VCAP::CloudController::ApiSpecHelper
shared_examples "deleting a valid object" do |opts|
describe "deleting a valid object" do
describe "DELETE #{opts[:path]}/:id" do
- let (:obj) { opts[:model].make }
-
before(:all) do
- delete "#{opts[:path]}/#{obj.guid}", {}, admin_headers
+ @obj = opts[:model].make
+ delete "#{opts[:path]}/#{@obj.guid}", {}, admin_headers
end
it "should return 204" do
@@ -16,9 +16,7 @@ module VCAP::CloudController::ApiSpecHelper
path = "#{path}#{qp.join("&")}"
describe "GET #{path}" do
- before(:all) do
- get "#{path}", {}, json_headers(admin_headers)
- end
+ before(:all) { get "#{path}", {}, json_headers(admin_headers) }
it "should return 200" do
last_response.status.should == 200
@@ -4,28 +4,24 @@ module VCAP::CloudController::ApiSpecHelper
shared_examples "operations on an invalid object" do |opts|
describe "operations on an invalid object" do
describe "POST #{opts[:path]}/:invalid_id/" do
- before(:all) do
- post "#{opts[:path]}/999999", {}, json_headers(admin_headers)
- end
+ before(:all) { post "#{opts[:path]}/999999", {}, json_headers(admin_headers) }
- it "should return 404" do
- last_response.status.should == 404
- end
+ it "should return 404" do
+ last_response.status.should == 404
+ end
- it_behaves_like "a vcap rest error response", /Unknown request/
+ it_behaves_like "a vcap rest error response", /Unknown request/
end
[:put, :delete, :get].each do |verb|
describe "#{verb.upcase} #{opts[:path]}/:invalid_id/" do
- before(:all) do
- send(verb, "#{opts[:path]}/999999", {}, json_headers(admin_headers))
- end
+ before(:all) { send(verb, "#{opts[:path]}/999999", {}, json_headers(admin_headers)) }
- it "should return 404" do
- last_response.status.should == 404
- end
+ it "should return 404" do
+ last_response.status.should == 404
+ end
- it_behaves_like "a vcap rest error response", "not be found: 999999"
+ it_behaves_like "a vcap rest error response", "not be found: 999999"
end
end
end
@@ -3,21 +3,18 @@
module VCAP::CloudController::ApiSpecHelper
shared_examples "querying objects" do |opts|
describe "querying objects" do
- before(:all) do
- 5.times { opts[:model].make }
- end
+ before(:all) { 5.times { opts[:model].make } }
opts[:queryable_attributes].each do |attr|
describe "#{opts[:path]}?q=#{attr}:<val>" do
- let(:val) { Sham.send(attr) }
-
before(:all) do
- opts[:model].make(attr => val)
+ @val = Sham.send(attr)
+ opts[:model].make(attr => @val)
end
describe "with a matching value" do
before(:all) do
- get "#{opts[:path]}?q=#{attr}:#{val}", {}, json_headers(admin_headers)
+ get "#{opts[:path]}?q=#{attr}:#{@val}", {}, json_headers(admin_headers)
end
it "should return 200" do
@@ -30,9 +27,9 @@ module VCAP::CloudController::ApiSpecHelper
end
describe "with a non-existent value" do
- let(:non_existent_value) { Sham.send(attr) }
before(:all) do
- get "#{opts[:path]}?q=#{attr}:#{non_existent_value}", {}, json_headers(admin_headers)
+ @non_existent_value = Sham.send(attr)
+ get "#{opts[:path]}?q=#{attr}:#{@non_existent_value}", {}, json_headers(admin_headers)
end
it "should return 200" do
@@ -4,10 +4,9 @@ module VCAP::CloudController::ApiSpecHelper
shared_examples "reading a valid object" do |opts|
describe "reading a valid object" do
describe "GET #{opts[:path]}/:id" do
- let (:obj) { opts[:model].make }
-
before(:all) do
- get "#{opts[:path]}/#{obj.guid}", {}, json_headers(admin_headers)
+ @obj = opts[:model].make
+ get "#{opts[:path]}/#{@obj.guid}", {}, json_headers(admin_headers)
end
it "should return 200" do
@@ -17,7 +16,7 @@ module VCAP::CloudController::ApiSpecHelper
include_examples "return a vcap rest encoded object"
it "should return the json encoded object in the response body" do
- expected = obj.to_hash
+ expected = @obj.to_hash
expected.each { |k, v| expected[k] = v.to_s if v.kind_of?(Time) }
# filter out the relation urls.
Oops, something went wrong.

0 comments on commit 69f32c1

Please sign in to comment.