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')
12 require_project_file('twitter/rails')
14 # Add helper methods here if relevant to multiple _spec.rb files
16 # Spec helper that sets attribute <tt>att</tt> for given objects <tt>obj</tt>
17 # and <tt>other</tt> to given <tt>value</tt>.
18 def equalizer(obj, other, att, value)
20 obj.send(setter, value)
21 other.send(setter, value)
24 # Spec helper that nil-izes objects passed in
26 objects.each {|obj| obj = nil }
29 # Returns default <tt>client</tt> context object
30 def client_context(file = 'config/twitter.yml')
31 Twitter::Client.from_config(file)
34 # Spec helper that returns a mocked <tt>Twitter::Config</tt> object
35 # with stubbed attributes and <tt>attrs</tt> for overriding attribute
37 def stubbed_twitter_config(config, attrs = {})
40 :host => 'twitter.com',
42 :proxy_host => 'proxy.host',
45 config.stub!(:protocol).and_return(opts[:protocol])
46 config.stub!(:host).and_return(opts[:host])
47 config.stub!(:port).and_return(opts[:port])
48 config.stub!(:proxy_host).and_return(opts[:proxy_host])
49 config.stub!(:proxy_port).and_return(opts[:proxy_port])
53 def mas_twitter_config(attrs = {})
54 config = mock(Twitter::Config)
55 stubbed_twitter_conf(config, attrs)
58 def stubbed_twitter_status(status, attrs = {})
62 status.stub!(:id).and_return(opts[:id])
63 status.stub!(:to_i).and_return(opts[:id])
64 (opts.keys - [:id]).each do |att|
65 status.stub!(att).and_return(opts[att])
67 status.stub!(:bless).and_return(nil)
71 def mas_twitter_status(attrs = {})
72 status = mock(Twitter::Status)
73 stubbed_twitter_status(status, attrs)
76 # Spec helper that returns the project root directory as absolute path string
78 File.expand_path(File.join(File.dirname(__FILE__), '..'))
81 # Spec helper that returns stubbed <tt>Net::HTTP</tt> object
82 # with given <tt>response</tt> and <tt>obj_stubs</tt>.
83 # The <tt>host</tt> and <tt>port</tt> are used to initialize
84 # the Net::HTTP object.
85 def stubbed_net_http(response, obj_stubs = {}, host = 'twitter.com', port = 80)
86 http = Net::HTTP.new(host, port)
87 Net::HTTP.stub!(:new).and_return(http)
88 http.stub!(:request).and_return(response)
92 # Spec helper that returns a mocked <tt>Net::HTTP</tt> object and
93 # stubs out the <tt>request</tt> method to return the given
95 def mas_net_http(response, obj_stubs = {})
96 http = mock(Net::HTTP, obj_stubs)
97 Net::HTTP.stub!(:new).and_return(http)
98 http.stub!(:request).and_return(response)
99 http.stub!(:start).and_yield(http)
100 http.stub!(:use_ssl=)
101 http.stub!(:verify_mode=)
105 # Spec helper that returns a mocked <tt>Net::HTTP::Get</tt> object and
106 # stubs relevant class methods and given <tt>obj_stubs</tt>
108 def mas_net_http_get(obj_stubs = {})
109 request = Spec::Mocks::Mock.new(Net::HTTP::Get)
110 Net::HTTP::Get.stub!(:new).and_return(request)
111 obj_stubs.each do |method, value|
112 request.stub!(method).and_return(value)
117 # Spec helper that returns a mocked <tt>Net::HTTP::Post</tt> object and
118 # stubs relevant class methods and given <tt>obj_stubs</tt>
120 def mas_net_http_post(obj_stubs = {})
121 request = Spec::Mocks::Mock.new(Net::HTTP::Post)
122 Net::HTTP::Post.stub!(:new).and_return(request)
123 obj_stubs.each do |method, value|
124 request.stub!(method).and_return(value)
129 # Spec helper that returns a mocked <tt>Net::HTTPResponse</tt> object and
130 # stubs given <tt>obj_stubs</tt> for endo-specing.
132 def mas_net_http_response(status = :success,
135 response = Spec::Mocks::Mock.new(Net::HTTPResponse)
136 response.stub!(:body).and_return(body)
139 _create_http_response(response, "200", "OK")
141 _create_http_response(response, "201", "Created")
142 when :redirect || 301
143 _create_http_response(response, "301", "Redirect")
144 when :not_authorized || 401
145 _create_http_response(response, "401", "Not Authorized")
146 when :forbidden || 403
147 _create_http_response(response, "403", "Forbidden")
148 when :file_not_found || 404
149 _create_http_response(response, "404", "File Not Found")
150 when :server_error || 500
151 _create_http_response(response, "500", "Server Error")
156 # Local helper method to DRY up code.
157 def _create_http_response(mock_response, code, message)
158 mock_response.stub!(:code).and_return(code)
159 mock_response.stub!(:message).and_return(message)
160 mock_response.stub!(:is_a?).and_return(true) if ["200", "201"].member?(code)