1 $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
5 def require_project_file(file)
6 require(File.join(File.dirname(__FILE__), '..', 'lib', file))
9 require_project_file('twitter')
10 require_project_file('twitter/console')
11 require_project_file('twitter/extras')
13 # Add helper methods here if relevant to multiple _spec.rb files
15 # Spec helper that sets attribute <tt>att</tt> for given objects <tt>obj</tt>
16 # and <tt>other</tt> to given <tt>value</tt>.
17 def equalizer(obj, other, att, value)
19 obj.send(setter, value)
20 other.send(setter, value)
23 # Spec helper that nil-izes objects passed in
25 objects.each {|obj| obj = nil }
28 # Returns default <tt>client</tt> context object
29 def client_context(file = 'config/twitter.yml')
30 Twitter::Client.from_config(file)
33 # Spec helper that returns a mocked <tt>Twitter::Config</tt> object
34 # with stubbed attributes and <tt>attrs</tt> for overriding attribute
36 def stubbed_twitter_config(config, attrs = {})
39 :host => 'twitter.com',
41 :proxy_host => 'proxy.host',
44 config.stub!(:protocol).and_return(opts[:protocol])
45 config.stub!(:host).and_return(opts[:host])
46 config.stub!(:port).and_return(opts[:port])
47 config.stub!(:proxy_host).and_return(opts[:proxy_host])
48 config.stub!(:proxy_port).and_return(opts[:proxy_port])
52 def mas_twitter_config(attrs = {})
53 config = mock(Twitter::Config)
54 stubbed_twitter_conf(config, attrs)
57 def stubbed_twitter_status(status, attrs = {})
61 status.stub!(:id).and_return(opts[:id])
62 status.stub!(:to_i).and_return(opts[:id])
63 (opts.keys - [:id]).each do |att|
64 status.stub!(att).and_return(opts[att])
66 status.stub!(:bless).and_return(nil)
70 def mas_twitter_status(attrs = {})
71 status = mock(Twitter::Status)
72 stubbed_twitter_status(status, attrs)
75 # Spec helper that returns the project root directory as absolute path string
77 File.expand_path(File.join(File.dirname(__FILE__), '..'))
80 # Spec helper that returns stubbed <tt>Net::HTTP</tt> object
81 # with given <tt>response</tt> and <tt>obj_stubs</tt>.
82 # The <tt>host</tt> and <tt>port</tt> are used to initialize
83 # the Net::HTTP object.
84 def stubbed_net_http(response, obj_stubs = {}, host = 'twitter.com', port = 80)
85 http = Net::HTTP.new(host, port)
86 Net::HTTP.stub!(:new).and_return(http)
87 http.stub!(:request).and_return(response)
91 # Spec helper that returns a mocked <tt>Net::HTTP</tt> object and
92 # stubs out the <tt>request</tt> method to return the given
94 def mas_net_http(response, obj_stubs = {})
95 http = mock(Net::HTTP, obj_stubs)
96 Net::HTTP.stub!(:new).and_return(http)
97 http.stub!(:request).and_return(response)
98 http.stub!(:start).and_yield(http)
100 http.stub!(:verify_mode=)
104 # Spec helper that returns a mocked <tt>Net::HTTP::Get</tt> object and
105 # stubs relevant class methods and given <tt>obj_stubs</tt>
107 def mas_net_http_get(obj_stubs = {})
108 request = Spec::Mocks::Mock.new(Net::HTTP::Get)
109 Net::HTTP::Get.stub!(:new).and_return(request)
110 obj_stubs.each do |method, value|
111 request.stub!(method).and_return(value)
116 # Spec helper that returns a mocked <tt>Net::HTTP::Post</tt> object and
117 # stubs relevant class methods and given <tt>obj_stubs</tt>
119 def mas_net_http_post(obj_stubs = {})
120 request = Spec::Mocks::Mock.new(Net::HTTP::Post)
121 Net::HTTP::Post.stub!(:new).and_return(request)
122 obj_stubs.each do |method, value|
123 request.stub!(method).and_return(value)
128 # Spec helper that returns a mocked <tt>Net::HTTPResponse</tt> object and
129 # stubs given <tt>obj_stubs</tt> for endo-specing.
131 def mas_net_http_response(status = :success,
134 response = Spec::Mocks::Mock.new(Net::HTTPResponse)
135 response.stub!(:body).and_return(body)
138 _create_http_response(response, "200", "OK")
140 _create_http_response(response, "201", "Created")
141 when :redirect || 301
142 _create_http_response(response, "301", "Redirect")
143 when :not_authorized || 401
144 _create_http_response(response, "401", "Not Authorized")
145 when :forbidden || 403
146 _create_http_response(response, "403", "Forbidden")
147 when :file_not_found || 404
148 _create_http_response(response, "404", "File Not Found")
149 when :server_error || 500
150 _create_http_response(response, "500", "Server Error")
155 # Local helper method to DRY up code.
156 def _create_http_response(mock_response, code, message)
157 mock_response.stub!(:code).and_return(code)
158 mock_response.stub!(:message).and_return(message)
159 mock_response.stub!(:is_a?).and_return(true) if ["200", "201"].member?(code)