【问题标题】:How best to DRY up this Rspec code?如何最好地干燥这个 Rspec 代码?
【发布时间】:2012-09-06 21:31:12
【问题描述】:

我有以下 Rspec 代码,我认为可以将其干掉很多,特别是 check_creation_email_sent 和 check_rename_email_sent 方法,我只是不知道该怎么做。

我尝试使用 shared_examples 和 it_behaves_like 进行此操作,但收到错误 undefined method it_behaves_like... 所以我希望有人可以为我解惑。

require 'spec_helper'

describe CreateTopicFolder do
  let(:ids) { [123,456] }

  def clear_bucket(topic_id)
    subject.find_folder(topic_id).each { |obj| obj.delete } unless subject.find_folder(topic_id).nil?
  end

  def check_folder_and_object(topic_id, permalink, old_permalink=nil)
    folder = subject.find_folder(topic_id)
    folder.should_not be_empty
    folder.size.should == 1

    object = folder.first
    object.key.should include(permalink)
    object.key.should_not include(old_permalink) unless old_permalink.nil?
  end

  def check_creation_email_sent(email, permalink)
    ActionMailer::Base.deliveries.should_not be_empty

    email.subject.should match("Folder for '#{permalink}' created")
    email.body.should include("(You can reply to this e-mail to contact Engineering if something doesn't look right)")
  end

  def check_rename_email_sent(email, permalink, old_permalink)
    ActionMailer::Base.deliveries.should_not be_empty

    email.subject.should match("Folder for '#{old_permalink}' renamed to '#{permalink}'")
    email.body.should include("(You can reply to this e-mail to contact Engineering if something doesn't look right)")
  end

  before(:all) do
    ids.each { |folder| clear_bucket(folder) }
    permalink = "123-How-to-Have-Fun"
    subject.create_or_rename_folder(permalink)
  end

  it 'should find an existing folder in the S3 bucket if it exists' do
    subject.find_folder(123).should_not be_empty
  end

  it 'should create a new folder with the topic permalink as name if none already exists' do
    subject.find_folder(456).should be_nil

    permalink = "456-How-to-Not-Have-Any-Fun"
    subject.create_or_rename_folder(permalink)
    email = subject.instance_variable_get(:@email)

    check_creation_email_sent(email, permalink)
    check_folder_and_object(456, permalink)
  end

  it 'should rename an existing folder with new permalink' do
    subject.find_folder(456).should_not be_empty

    permalink = "456-How-to-Have-Fun-Again"
    old_permalink = subject.instance_variable_get(:@old_permalink)
    subject.create_or_rename_folder(permalink)
    email = subject.instance_variable_get(:@email)

    check_rename_email_sent(email, permalink, old_permalink)
    check_folder_and_object(456, permalink, old_permalink)
  end
end

提前感谢您的帮助!

【问题讨论】:

    标签: ruby-on-rails rspec refactoring dry


    【解决方案1】:

    您可能正在寻找shared_examples_for("a thing") 和it_should_behave_like("a thing")。

    【讨论】:

    • 是的,我试过了,但得到了我在帖子中提到的undefined method 错误。
    • 你说你试过shared_examples和it_behaves_like,而不是it_should_behave_like
    • 很抱歉。我试过shared_examples/shared_examples_for 和it_behaves_like/it_should_behave_like。每种组合都有相同的错误。
    • 如何将check_folder_and_object 和check_rename_email_sent 重构为rspec matchers 以便您可以使用it { should send_rename_email } 或it { should send_rename_email.with_email(@email) } 等?
    • 我会先试一试!
    猜你喜欢
    • 1970-01-01
    • 2013-09-16
    • 1970-01-01
    • 2011-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-18
    • 2016-09-03
    相关资源
    最近更新 更多