0

I have the an instance method of the class Schedule:

However, I would like one of the methods create_recurring to create an object of class Orchestrate.

The reason why is that I wanted all classes of Orchestrate to be what creates something in the actual database. That class has all the methods to actually create something.

So it looks like this:

def create_recurring
    orchestrate = OrchestrateIo.new(@bot_client_id, :profiles)
end

However, when writing my rspec, I had no idea how to actually test for the creation of another object, which made me think I was going about it all wrong.

questions: 1) Is this doable/allowable? 2) If yes, how would I test for the creation of a new instance of another Class? 3) What's the right way to do it if this approach is wrong?

3
  • Of course, you can use instance methods to create objects. Since Ruby is an object-oriented language, there's pretty much only two things you can do: create objects or send messages to objects (actually, you typically create objects by sending messages to objects, e.g. in your example, you create an instance of OrchestrateIo by sending the message new to OrchestrateIo). Also, all methods in Ruby are instance methods, there is no other kind of method. Now, since all methods are instance methods, and creating objects is (half of) all you do in Ruby, it would be extremely stupid, if … Commented Oct 9, 2015 at 5:31
  • … you couldn't create objects in instance methods. And in fact, in the code you posted, you already demonstrate that it is possible to create objects in instance methods, since you are creating not one, but two objects in your method: one instance of OrchestrateIo, and one instance of Symbol (:profiles). The latter demonstrates another of creating objects: literals. Commented Oct 9, 2015 at 5:33
  • How would I then test that I correctly created it? Commented Oct 9, 2015 at 5:42

1 Answer 1

1

1) Of course it is doable. It is the core of the factory pattern.

2) something like this? (not 100% sure about your code structure, so...)

it "makes an OrchestrateIo" do
  Orchestrate.create_recurring.should be_an_instance_of(OrchestrateIo)
end

3) It's not wrong. It may or may not be appropriate for what you are doing, but there is not enough context to the question (or I am not smart enough) for me to figure out if it is, or not.

Sign up to request clarification or add additional context in comments.

4 Comments

Hi. Create recurring is a method for Schedule. I have done some additional reading. Seems like my options are to make schedule a sub class of Orchestrate.
Or the alternative is to create a separate object without relying on the Orchcestrate class.
I read your factory link. Yes that describes what I want to do. I guess I am not clear how to test it. My rspec describes one class. But I need to test for the creation of another object of another class.
I thought the rspec snippet I posted tests exactly that? (Unless I messed up the syntax)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.