vkontakte_api 1.2 → 1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,51 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe VkontakteApi::Namespace do
4
- # nothing to spec here yet
4
+ describe ".names" do
5
+ before(:each) do
6
+ VkontakteApi::Namespace.instance_variable_set(:@names, nil)
7
+ end
8
+
9
+ context "on first call" do
10
+ it "loads namespaces from a file" do
11
+ filename = double("Filename")
12
+ expect(File).to receive(:expand_path).and_return(filename)
13
+ namespaces = double("Namespaces list")
14
+ expect(YAML).to receive(:load_file).with(filename).and_return(namespaces)
15
+
16
+ VkontakteApi::Namespace.names
17
+ end
18
+ end
19
+
20
+ context "on subsequent calls" do
21
+ before(:each) do
22
+ VkontakteApi::Namespace.names
23
+ end
24
+
25
+ it "returns the cached list" do
26
+ expect(YAML).not_to receive(:load_file)
27
+ VkontakteApi::Namespace.names
28
+ end
29
+ end
30
+ end
31
+
32
+ describe ".exists?" do
33
+ context "with an existing namespace" do
34
+ it "returns true" do
35
+ expect(VkontakteApi::Namespace).to exist('users')
36
+ end
37
+ end
38
+
39
+ context "with a non-existent namespace" do
40
+ it "returns false" do
41
+ expect(VkontakteApi::Namespace).not_to exist('admins')
42
+ end
43
+ end
44
+
45
+ context "with an existing symbol namespace" do
46
+ it "returns true" do
47
+ expect(VkontakteApi::Namespace).to exist(:users)
48
+ end
49
+ end
50
+ end
5
51
  end
@@ -8,14 +8,12 @@ describe VkontakteApi::Resolvable do
8
8
  end
9
9
 
10
10
  describe "#initialize" do
11
- it "should save name and resolver" do
12
- name = double("Name")
13
- token = double("Token")
14
- resolver = Hashie::Mash.new(token: token)
11
+ it "saves the name and the resolver" do
12
+ resolver = Hashie::Mash.new(token: 'token')
13
+ resolvable = @class.new(:name, resolver: resolver)
15
14
 
16
- resolvable = @class.new(name, resolver: resolver)
17
- resolvable.name.should == name
18
- resolvable.token.should == token
15
+ expect(resolvable.name).to eq('name')
16
+ expect(resolvable.token).to eq('token')
19
17
  end
20
18
  end
21
19
  end
@@ -11,39 +11,6 @@ describe VkontakteApi::Resolver do
11
11
  end
12
12
  end
13
13
 
14
- describe "#method_missing" do
15
- before(:each) do
16
- @resolver = @class.new(:trololo)
17
- @token = double("Token")
18
- @resolver.stub(:token).and_return(@token)
19
- end
20
-
21
- context "called with a namespace" do
22
- it "returns a Namespace instance" do
23
- namespace = @resolver.users
24
- namespace.should be_a(VkontakteApi::Namespace)
25
- end
26
- end
27
-
28
- context "called with a method" do
29
- before(:each) do
30
- @result = double("Result")
31
- @method = double("Method", call: @result)
32
- VkontakteApi::Method.stub(:new).and_return(@method)
33
- end
34
-
35
- it "creates a Method instance" do
36
- VkontakteApi::Method.should_receive(:new).with('get', resolver: @resolver.resolver)
37
- @resolver.get(id: 1)
38
- end
39
-
40
- it "calls Method#call and returns the result" do
41
- @method.should_receive(:call).with(id: 1)
42
- @resolver.get(id: 1).should == @result
43
- end
44
- end
45
- end
46
-
47
14
  describe "#send" do
48
15
  before(:each) do
49
16
  @resolver = @class.new('trololo')
@@ -53,7 +20,7 @@ describe VkontakteApi::Resolver do
53
20
 
54
21
  it "gets into #method_missing" do
55
22
  method = double("Method", call: nil)
56
- VkontakteApi::Method.should_receive(:new).with('send', resolver: @resolver.resolver).and_return(method)
23
+ expect(@resolver).to receive(:method_missing).with(:send, message: 'hello')
57
24
  @resolver.send(message: 'hello')
58
25
  end
59
26
  end
@@ -66,44 +33,17 @@ describe VkontakteApi::Resolver do
66
33
  @resolver.stub(:token).and_return(@token)
67
34
  end
68
35
 
36
+ let(:resolver) { @resolver.resolver }
37
+
69
38
  it "returns a Hashie::Mash with a name and a token" do
70
- r = @resolver.resolver
71
- r.name.should == @name
72
- r.token.should == @token
39
+ expect(resolver.name).to eq(@name)
40
+ expect(resolver.token).to eq(@token)
73
41
  end
74
42
 
75
43
  it "caches the result" do
76
44
  @mash = double("Mash", name: @name, token: @token)
77
- Hashie::Mash.should_receive(:new).once.and_return(@mash)
45
+ expect(Hashie::Mash).to receive(:new).once.and_return(@mash)
78
46
  5.times { @resolver.resolver }
79
47
  end
80
48
  end
81
-
82
- describe ".namespaces" do
83
- before(:each) do
84
- VkontakteApi::Resolver.instance_variable_set(:@namespaces, nil)
85
- end
86
-
87
- context "on first call" do
88
- it "loads namespaces from a file" do
89
- filename = double("Filename")
90
- File.should_receive(:expand_path).and_return(filename)
91
- namespaces = double("Namespaces list")
92
- YAML.should_receive(:load_file).with(filename).and_return(namespaces)
93
-
94
- VkontakteApi::Resolver.namespaces
95
- end
96
- end
97
-
98
- context "on subsequent calls" do
99
- before(:each) do
100
- VkontakteApi::Resolver.namespaces
101
- end
102
-
103
- it "returns the cached list" do
104
- YAML.should_not_receive(:load_file)
105
- VkontakteApi::Resolver.namespaces
106
- end
107
- end
108
- end
109
49
  end
@@ -2,90 +2,99 @@ require 'spec_helper'
2
2
 
3
3
  describe VkontakteApi::Result do
4
4
  describe ".process" do
5
+ let(:response) { double("Response") }
6
+ let(:result) { double("Result") }
7
+
5
8
  before(:each) do
6
- @response = double("Response")
7
- @result = double("Result")
8
- subject.stub(:extract_result).and_return(@result)
9
+ subject.stub(:extract_result).and_return(result)
9
10
  end
10
11
 
11
12
  it "calls .extract_result passing it the response" do
12
- subject.should_receive(:extract_result).with(@response)
13
- subject.process(@response, @type, nil)
13
+ expect(subject).to receive(:extract_result).with(response)
14
+ subject.process(response, :anything, nil)
14
15
  end
15
16
 
16
17
  context "with a non-enumerable result" do
18
+ let(:type) { double("Type") }
19
+ let(:typecasted_value) { double("Typecasted value") }
20
+
17
21
  before(:each) do
18
- @type = double("Type")
19
- @typecasted_value = double("Typecasted value")
20
- subject.stub(:typecast).and_return(@typecasted_value)
22
+ subject.stub(:typecast).and_return(typecasted_value)
21
23
  end
22
24
 
23
25
  it "returns #typecast-ed value" do
24
- subject.should_receive(:typecast).with(@result, @type).and_return(@typecasted_value)
25
- subject.send(:process, @result, @type, nil).should == @typecasted_value
26
+ expect(subject).to receive(:typecast).with(result, type).and_return(typecasted_value)
27
+ expect(subject.process(result, type, nil)).to eq(typecasted_value)
26
28
  end
27
29
 
28
30
  context "when block_given?" do
29
31
  it "yields the #typecast-ed value and returns the result of the block" do
30
32
  block_result = double("Block result")
31
- @typecasted_value.should_receive(:result_method).and_return(block_result)
33
+ expect(typecasted_value).to receive(:result_method).and_return(block_result)
32
34
  block = proc(&:result_method)
33
35
 
34
- subject.send(:process, @response, @type, block).should == block_result
36
+ expect(subject.process(response, type, block)).to eq(block_result)
35
37
  end
36
38
  end
37
39
  end
38
40
 
39
41
  context "with an enumerable result" do
42
+ let(:element1) { double("First element") }
43
+ let(:element2) { double("Second element") }
44
+ let(:enumerable_result) { [element1, element2] }
45
+
40
46
  before(:each) do
41
- @element1 = double("First element")
42
- @element2 = double("Second element")
43
- @enumerable_result = [@element1, @element2]
44
- subject.stub(:extract_result).and_return(@enumerable_result)
47
+ subject.stub(:extract_result).and_return(enumerable_result)
45
48
  end
46
49
 
47
50
  it "returns the untouched value" do
48
- subject.send(:process, @enumerable_result, :anything, nil).should == @enumerable_result
51
+ expect(subject.process(enumerable_result, :anything, nil)).to eq(enumerable_result)
49
52
  end
50
53
 
51
54
  context "when block_given?" do
52
55
  it "yields each element untouched to the block" do
53
56
  result1 = double("First element after result_method")
54
57
  result2 = double("Second element after result_method")
55
- @element1.should_receive(:result_method).and_return(result1)
56
- @element2.should_receive(:result_method).and_return(result2)
58
+ expect(element1).to receive(:result_method).and_return(result1)
59
+ expect(element2).to receive(:result_method).and_return(result2)
57
60
  block = proc(&:result_method)
58
61
 
59
- subject.send(:process, @enumerable_result, :anything, block).should == [result1, result2]
62
+ expect(subject.process(enumerable_result, :anything, block)).to eq([result1, result2])
60
63
  end
61
64
  end
62
65
  end
63
66
  end
64
67
 
65
68
  describe ".extract_result" do
66
- before(:each) do
67
- @result_response = { 'key' => 'value' }
68
- @result_error = { 'request_params' => [{ 'key' => 'error', 'value' => 'description' }] }
69
+ let(:result_response) do
70
+ { 'key' => 'value' }
71
+ end
72
+
73
+ let(:result_error) do
74
+ {
75
+ 'request_params' => [
76
+ {
77
+ 'key' => 'error',
78
+ 'value' => 'description'
79
+ }
80
+ ]
81
+ }
69
82
  end
70
83
 
71
84
  context "with a successful response" do
72
- before(:each) do
73
- @result = Hashie::Mash.new(response: @result_response)
74
- end
85
+ let(:result) { Hashie::Mash.new(response: result_response) }
75
86
 
76
87
  it "returns the response part" do
77
- subject.send(:extract_result, @result).should == @result_response
88
+ expect(subject.send(:extract_result, result)).to eq(result_response)
78
89
  end
79
90
  end
80
91
 
81
92
  context "with an error response" do
82
- before(:each) do
83
- @result = Hashie::Mash.new(error: @result_error)
84
- end
93
+ let(:result) { Hashie::Mash.new(error: result_error) }
85
94
 
86
95
  it "raises a VkontakteApi::Error" do
87
96
  expect {
88
- subject.send(:extract_result, @result)
97
+ subject.send(:extract_result, result)
89
98
  }.to raise_error(VkontakteApi::Error)
90
99
  end
91
100
  end
@@ -94,20 +103,20 @@ describe VkontakteApi::Result do
94
103
  describe ".typecast" do
95
104
  context "with an :anything type" do
96
105
  it "returns it's argument untouched" do
97
- subject.send(:typecast, :some_arg, :anything).should == :some_arg
106
+ expect(subject.send(:typecast, :some_arg, :anything)).to eq(:some_arg)
98
107
  end
99
108
  end
100
109
 
101
110
  context "with a :boolean type" do
102
111
  context "and zero result" do
103
112
  it "returns false" do
104
- subject.send(:typecast, '0', :boolean).should == false
113
+ expect(subject.send(:typecast, '0', :boolean)).to eq(false)
105
114
  end
106
115
  end
107
116
 
108
117
  context "and non-zero parameter" do
109
118
  it "returns true" do
110
- subject.send(:typecast, '1', :boolean).should == true
119
+ expect(subject.send(:typecast, '1', :boolean)).to eq(true)
111
120
  end
112
121
  end
113
122
  end
@@ -1,26 +1,27 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe VkontakteApi::Uploading do
4
- before(:each) do
5
- @uploader = Object.new
6
- @uploader.extend VkontakteApi::Uploading
4
+ let(:uploader) do
5
+ Object.new.tap do |object|
6
+ object.extend VkontakteApi::Uploading
7
+ end
7
8
  end
8
9
 
9
10
  describe "#upload" do
11
+ let(:upload_io) { double("Faraday::UploadIO instance") }
12
+ let(:response_body) { double("Server response body") }
13
+ let(:response) { double("Server response", body: response_body) }
14
+ let(:connection) { double("Faraday connection", post: response) }
15
+
10
16
  before(:each) do
11
- @upload_io = double("Faraday::UploadIO instance")
12
- Faraday::UploadIO.stub(:new).and_return(@upload_io)
13
-
14
- @response_body = double("Server response body")
15
- response = double("Server response", body: @response_body)
16
- @connection = double("Faraday connection", post: response)
17
- VkontakteApi::API.stub(:connection).and_return(@connection)
17
+ Faraday::UploadIO.stub(:new).and_return(upload_io)
18
+ VkontakteApi::API.stub(:connection).and_return(connection)
18
19
  end
19
20
 
20
21
  context "without a :url param" do
21
22
  it "raises an ArgumentError" do
22
23
  expect {
23
- @uploader.upload
24
+ uploader.upload
24
25
  }.to raise_error(ArgumentError)
25
26
  end
26
27
  end
@@ -28,19 +29,22 @@ describe VkontakteApi::Uploading do
28
29
  it "creates a Faraday::UploadIO for each file passed in" do
29
30
  path = double("File path")
30
31
  type = double("File mime type")
31
- Faraday::UploadIO.should_receive(:new).with(path, type)
32
- @uploader.upload(url: 'http://example.com', file1: [path, type])
32
+ io = double("File IO")
33
+
34
+ expect(Faraday::UploadIO).to receive(:new).with(path, type, nil)
35
+ expect(Faraday::UploadIO).to receive(:new).with(io, type, path)
36
+ uploader.upload(url: 'http://example.com', file1: [path, type], file2: [io, type, path])
33
37
  end
34
38
 
35
39
  it "POSTs the files through the connection to a given URL" do
36
40
  url = double("URL")
37
41
  file = double("File")
38
- @connection.should_receive(:post).with(url, file1: @upload_io)
39
- @uploader.upload(url: url, file1: file)
42
+ expect(connection).to receive(:post).with(url, file1: upload_io)
43
+ uploader.upload(url: url, file1: file)
40
44
  end
41
45
 
42
46
  it "returns the server response" do
43
- @uploader.upload(url: 'http://example.com').should == @response_body
47
+ expect(uploader.upload(url: 'http://example.com')).to eq(response_body)
44
48
  end
45
49
  end
46
50
  end
@@ -2,45 +2,42 @@ require 'spec_helper'
2
2
 
3
3
  describe VkontakteApi::Utils do
4
4
  describe ".flatten_arguments" do
5
+ let(:arg1) { double("First argument") }
6
+ let(:arg2) { double("Second argument") }
7
+ let(:flat_arg1) { double("Flattened first argument") }
8
+ let(:flat_arg2) { double("Flattened second argument") }
9
+
5
10
  before(:each) do
6
- @arg1 = double("First argument")
7
- @arg2 = double("Second argument")
8
- @flat_arg1 = double("Flattened first argument")
9
- @flat_arg2 = double("Flattened second argument")
10
-
11
11
  VkontakteApi::Utils.stub(:flatten_argument) do |arg|
12
12
  case arg
13
- when @arg1 then @flat_arg1
14
- when @arg2 then @flat_arg2
13
+ when arg1 then flat_arg1
14
+ when arg2 then flat_arg2
15
15
  end
16
16
  end
17
17
  end
18
18
 
19
19
  it "sends each value to .flatten_argument" do
20
- flat_arguments = VkontakteApi::Utils.flatten_arguments(arg1: @arg1, arg2: @arg2)
21
- flat_arguments.should == { arg1: @flat_arg1, arg2: @flat_arg2 }
20
+ flat_arguments = VkontakteApi::Utils.flatten_arguments(arg1: arg1, arg2: arg2)
21
+ expect(flat_arguments).to eq(arg1: flat_arg1, arg2: flat_arg2)
22
22
  end
23
23
  end
24
24
 
25
25
  describe ".flatten_argument" do
26
26
  context "with a flat argument" do
27
- before(:each) do
28
- @argument = :flat
29
- end
27
+ let(:argument) { :flat }
30
28
 
31
29
  it "leaves it untouched" do
32
- subject.send(:flatten_argument, @argument).should == @argument
30
+ result = subject.send(:flatten_argument, argument)
31
+ expect(result).to eq(argument)
33
32
  end
34
33
  end
35
34
 
36
35
  context "with an array argument" do
37
- before(:each) do
38
- @array_argument = [1, 2, 3]
39
- end
36
+ let(:array_argument) { [1, 2, 3] }
40
37
 
41
38
  it "joins the elements with a comma" do
42
- flat_argument = subject.send(:flatten_argument, @array_argument)
43
- flat_argument.should == '1,2,3'
39
+ flat_argument = subject.send(:flatten_argument, array_argument)
40
+ expect(flat_argument).to eq('1,2,3')
44
41
  end
45
42
  end
46
43
  end