从 Rspec 文件运行的代码的行为与从模型运行时的行为不同

发布于 2024-11-08 03:41:32 字数 1532 浏览 0 评论 0原文

我已经围绕这个问题转了几个小时了,但没有找到该问题的参考资料...

我们正在为存储在 AmazonS3 上的视频库构建一个简单的索引应用程序。 在编写测试时,我首先在测试文件上写入所有内容以确定我想要的结果,然后逐步将实际实现转移到模型中。

使用 Rails 3、AWS-S3 gem 和 Rspec 因此,在我的测试中,我从以下代码开始:

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

这个简单的测试创建一个知道 S3 存储桶名称和凭据的导入对象,并遍历存储桶中的对象并捕获对象的名称。这按预期工作。

当我将代码移至模型时,我最终得到以下模型;

app/models/s3import.rb

...

def objNames
  a = []
  bucket.objects.each do |i|
    a << i.key
  end
end

测试更改为:

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

我的困惑是,当我运行调用模型端代码的测试时,我没有得到我期望的字符串数组(正如我在独立测试代码中得到的那样)。我收到以下内容:

[#<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']

如您所见,返回的数组由原始 AWS::S3:S3Objects 组成...就好像循环只是复制原始数组,而不是将“键”作为字符串获取。

我在控制台中测试了相同的内容,但我似乎无法弄清楚导致差异的具体不同之处。

任何帮助将不胜感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魂归处 2024-11-15 03:41:32

我认为你正在归还水桶。尝试添加一行以获得不同的返回值。

def objNames
  a = []
  bucket.objects.each do |i|
    a << i.key
  end
  a
end

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文