Code run from Rspec file behaves differently than when run from the model
I've been spinning around this for the a few hours now without any luck finding a reference to the problem...
We're building a simple indexing app for a video library stored on AmazonS3. When writing my test, I initial write everything on the test file to establish what results I'd like, and progressively move the real implementation to the model.
Working with Rails 3, the AWS-S3 gem, and Rspec So, on my test, I start off with the following code:
spec/models/s3import_spec.rb
...
it "gets the names of all objects" do
im = S3import.new
a = []
im.bucket.objects.each do |obj|
a << obj.key
end
a.should == ["Agility/", "Agility/Stationary_Over Stick/",
"Agility/Stationary_Over Stick/2 foot hops over stick.mp4"]
end
This simple test creates an import object that knows the S3 bucket name and credentials, and goes through the objects in the bucket and captures the object's name. This works as expected.
When I move the code over to the model, I end up with the following model;
app/models/s3import.rb
...
def objNames
a = []开发者_开发技巧
bucket.objects.each do |i|
a << i.key
end
end
and the test changes to this:
it "gets the names of all objects" do
im = S3import.new
a = im.objNames
a.should == ["Agility/", "Agility/Stationary_Over Stick/",
"Agility/Stationary_Over Stick/2 foot hops over stick.mp4"]
end
My confusion is, when I run the test calling the code on the model side, I don't get the array of strings that I was expecting (as I got in the self-contained test code). I receive the following:
[#<AWS::S3::S3Object:0x2179225400 '/transcode2011/Agility/'>,
+ #<AWS::S3::S3Object:0x2179225380 '/transcode2011/Agility/Stationary_Over Stick/'>,
+ #<AWS::S3::S3Object:0x2179225320 '/transcode2011/Agility/Stationary_Over Stick/2 foot hops over stick.mp4']
As you can see, the returned array consist of the original AWS::S3:S3Objects... As if the loop simply duplicated the original array rather then getting the 'key' as a string.
I've tested the same in the console and I can't seem to figure out what specifically is different that causes the discrepancy.
Any help would be greatly appreciated.
I think you're returning the bucket. Try adding a line for a different return value.
def objNames
a = []
bucket.objects.each do |i|
a << i.key
end
a
end
精彩评论