Stubbing class inside class method

12 views
Skip to first unread message

Paulo Fabiano Langer

unread,
Feb 15, 2018, 3:41:53 PM2/15/18
to rspec
Hi, guys!

I'm struggling with a problem. 

Class X
   def y
      YAML.load_file(...)
   end
end

In my spec I have

allow(YAML).to receive(:load_file).and_return(...)
expect(obj.y).to eq ...

And it works!

But, if I change the method to

def self.y
   ...
end

and the expect to:

expect(X.y)...


It does not work :(

How can I make this stub and make it work with my class method?

Thanks,
Paulo

Myron Marston

unread,
Feb 15, 2018, 3:48:52 PM2/15/18
to rs...@googlegroups.com
Hi Paulo,

I can't reproduce your issue.  I ran your code (see this gist for the code I I'm running) and it passes as I would expect.  If you want someone to look more into this, please provide:
  • An explanation of what isn't working.  "It does not work" is hard to interpret.  Maybe it's just a misunderstanding.  Tell us what behavior you expect, and what behavior you are seeing, and if there's an exception, please provide the stacktrace.
  • Some example code we can download and run to reproduce the issue.  A gist like I provided is fine.
Thanks,
Myron

--
You received this message because you are subscribed to the Google Groups "rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rspec+unsubscribe@googlegroups.com.
To post to this group, send email to rs...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/46940dac-9fcc-4b6a-a6f2-7cfbbbe6e267%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages