1

我有以下方法:

class Topic
def create_or_rename_folder
  unless self.destroyed?
    bucket = CreateTopicFolder.new(bucket_name)
    bucket.create_or_rename_folder(permalink.split("/").last)
  end
end
...

这被称为:after_save :create_or_rename_folder, :if => :production_env?

我想测试一下,在创建一个TopicCreateTopicFolder实例时会创建一个新实例,并且如果aTopic被销毁,CreateTopicFolder不会创建一个新实例。

该类看起来像:

class CreateTopicFolder

   def initialize(bucket_name)
    s3 = AutoVideoAssociate
    s3.connect
    @bucket = s3.find_bucket(bucket_name)
  end
  ...

测试这个的正确方法是什么?

谢谢!

4

1 回答 1

0
context "When creating a Topic" do
  it "creates a CreateTopicFolder" do
    create_topic_folder = mock(CreateTopicFolder)
    CreateTopicFolder.stub(:new) { create_topic_folder }
    create_topic_folder.
      should_receive(:create_or_rename_folder).
      with("Sample Bucket Name")
    topic = Topic.new
    topic.bucket_name = "Sample Bucket Name"
    topic.save!
  end
end

context "When destroying a Topic" do
  it "does not create a CreateTopicFolder" do
    CreateTopicFolder.should_not_receive(:new)
    topic = mock_model(Topic)
    topic.destroy
  end
end
于 2012-09-18T03:24:08.277 回答