Added support for :page, :lite and :since options in Twitter::Client#my([:friends...
[twitter4r-core.git] / spec / spec_helper.rb
blobf503b03e768dd7db808cc120818413c99e5baae1
1 $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
3 require 'spec'
5 def require_project_file(file)
6   require(File.join(File.dirname(__FILE__), '..', 'lib', file))  
7 end
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)
19   setter = "#{att}="
20   obj.send(setter, value)
21   other.send(setter, value)
22 end
24 # Spec helper that nil-izes objects passed in
25 def nilize(*objects)
26   objects.each {|obj| obj = nil }
27 end
29 # Returns default <tt>client</tt> context object
30 def client_context(file = 'config/twitter.yml')
31   Twitter::Client.from_config(file)
32 end
34 # Spec helper that returns a mocked <tt>Twitter::Config</tt> object
35 # with stubbed attributes and <tt>attrs</tt> for overriding attribute 
36 # values.
37 def stubbed_twitter_config(config, attrs = {})
38   opts = { 
39     :protocol => :ssl,
40     :host => 'twitter.com',
41     :port => 443,
42     :proxy_host => 'proxy.host',
43     :proxy_port => 8080,
44   }.merge(attrs)
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])
50   config
51 end
53 def mas_twitter_config(attrs = {})
54   config = mock(Twitter::Config)
55   stubbed_twitter_conf(config, attrs)
56 end
58 def stubbed_twitter_status(status, attrs = {})
59   opts = {
60     :id => 23492343,
61   }.merge(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])
66   end
67   status.stub!(:bless).and_return(nil)
68   status
69 end
71 def mas_twitter_status(attrs = {})
72   status = mock(Twitter::Status)
73   stubbed_twitter_status(status, attrs)
74 end
76 # Spec helper that returns the project root directory as absolute path string
77 def project_root_dir
78   File.expand_path(File.join(File.dirname(__FILE__), '..'))
79 end
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)
89   http
90 end
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 
94 # <tt>response</tt>
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=)
102   http
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> 
107 # for endo-specing
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)
113   end
114   request
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> 
119 # for endo-specing
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)
125   end
126   request
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, 
133                           body = '', 
134                           obj_stubs = {})
135   response = Spec::Mocks::Mock.new(Net::HTTPResponse)
136   response.stub!(:body).and_return(body)
137   case status
138   when :success || 200
139     _create_http_response(response, "200", "OK")
140   when :created || 201
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")
152   end
153   response
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)