Factory_girl 和 sprintf

发布于 2024-12-18 07:26:58 字数 1930 浏览 1 评论 0原文

首先,我对 Ruby 还很陌生,尽管我有很强的 Java 背景(这里没有帮助:)。我创建了我的第一个 Rails 应用程序,并且正在使用 FactoryGirl。我遇到了一些奇怪的事情(对我来说),我无法弄清楚为什么它会这样。

在工厂中使用 sprintf (请参阅最后一个测试)会引发以下错误:

Failures:
  1) Test raises an ArgumentError
     Failure/Error: sprintf('Product %05d', n)
     ArgumentError:
       wrong number of arguments (3 for 2)
     # ./spec/models/fg_spec.rb:6:in `fff'
     # ./spec/models/fg_spec.rb:31:in `block (3 levels) in <top (required)>'
     # ./spec/models/fg_spec.rb:62:in `block (2 levels) in <top (required)>'

这是演示此行为的完整规范:

def fff(n)
    sprintf('WWW Product %05d', n)
end

b1 = proc { |n| fff(n) }
b2 = proc { |n| sprintf('WWW Product %05d', n) }

FactoryGirl.define do

    factory :product1, :class => Product do
        sequence(:name) { |n| 'Product %05d' % "#{n}" }
    end

    factory :product2, :class => Product do
        sequence(:name) { |n| sprintf('WWW Product %05d', n) }
    end

    factory :product3, :class => Product do
        sequence(:name, 1, &b1)
    end

    factory :product4, :class => Product do
        sequence(:name, 1, &b2)
    end

    factory :product5, :class => Product do
        sequence(:name) { |n| fff(n) }
    end

end

describe Test do

    it "works with %" do
        p = Factory.create(:product1)
        puts p.inspect
    end

    it "does not work with sprintf" do
        expect { Factory.create(:product2) }.to raise_error(ArgumentError)
    end

    it "works with a block with a function" do
        p = Factory.create(:product3)
        puts p.inspect
    end

    it "works with a block with sprintf" do
        p = Factory.create(:product4)
        puts p.inspect
    end

    it "does not work with a function with sprintf" do
        expect { Factory.create(:product5) }.to raise_error(ArgumentError)
    end

end

当然我可以使用 % 表示法,但我对此真的很好奇。

谢谢,

大卫

first of all, i am quite new to Ruby, although i have a strong background in Java (not helping here :). I created my first Rails application and i am using FactoryGirl. I came across something weird (for me) and i cannot figure out why it is behaving like this.

Using sprintf within a factory (see the last test) raises the following error :

Failures:
  1) Test raises an ArgumentError
     Failure/Error: sprintf('Product %05d', n)
     ArgumentError:
       wrong number of arguments (3 for 2)
     # ./spec/models/fg_spec.rb:6:in `fff'
     # ./spec/models/fg_spec.rb:31:in `block (3 levels) in <top (required)>'
     # ./spec/models/fg_spec.rb:62:in `block (2 levels) in <top (required)>'

Here is the full spec demonstrating this behavior:

def fff(n)
    sprintf('WWW Product %05d', n)
end

b1 = proc { |n| fff(n) }
b2 = proc { |n| sprintf('WWW Product %05d', n) }

FactoryGirl.define do

    factory :product1, :class => Product do
        sequence(:name) { |n| 'Product %05d' % "#{n}" }
    end

    factory :product2, :class => Product do
        sequence(:name) { |n| sprintf('WWW Product %05d', n) }
    end

    factory :product3, :class => Product do
        sequence(:name, 1, &b1)
    end

    factory :product4, :class => Product do
        sequence(:name, 1, &b2)
    end

    factory :product5, :class => Product do
        sequence(:name) { |n| fff(n) }
    end

end

describe Test do

    it "works with %" do
        p = Factory.create(:product1)
        puts p.inspect
    end

    it "does not work with sprintf" do
        expect { Factory.create(:product2) }.to raise_error(ArgumentError)
    end

    it "works with a block with a function" do
        p = Factory.create(:product3)
        puts p.inspect
    end

    it "works with a block with sprintf" do
        p = Factory.create(:product4)
        puts p.inspect
    end

    it "does not work with a function with sprintf" do
        expect { Factory.create(:product5) }.to raise_error(ArgumentError)
    end

end

Of course i could use the % notation but i am really curious about this.

Thanks,

David

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

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

发布评论

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

评论(1

べ映画 2024-12-25 07:26:58

您需要使用其完整命名空间来访问 sprintf:

b2 = proc { |n| Kernel.sprintf('WWW Product %05d', n) }

这是因为正在从 sprintf 未定义的不同上下文中调用相关代码。

You need to access sprintf using its full namespace:

b2 = proc { |n| Kernel.sprintf('WWW Product %05d', n) }

This is because the code in question is being called from within a different context where sprintf is undefined.

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