0

I'm struggling to figure out how to stub the following:

def max_post_limit
    Post.on(date).count > some_limit
end

I'm trying to stub the Post... section, not the method. So far I have tried the following and similar variations. All return either 0 or 1.

before do 
    Post.stubs(:on).returns([])
    Post.stubs.(:on).with(date).returns([])
    Post.stubs.(:count).returns(some_limit)
end

Any advice would be much appreciated, thank you.

user2420484
  • 123
  • 1
  • 7
  • letz's solution should work, but when you need to stub a chain of calls, it's often a sign that the interface between the classes could be improved. In this case, you could extract a method `Post.count_on_date(date)` which is then very easy to stub. – Andy Waite Jun 05 '15 at 06:29
  • The test suite is using mocha so i was going down the wrong path, apologies. I took your advice and created a `Post.created_on(date)` method and am now trying: post = Post.new post.expects(:created_on).with(date).returns(123) However, the error is 0 arguments for 1. Any advice on correctly passing the date variable? I thought `with` was doing that. Apparently not? – user2420484 Jun 05 '15 at 09:33
  • `created_on` is a class method, but it looks like you're stubbing an instance. I'm not sure of the mocha syntax, in RSpec it would be `allow(Post).to receive(:created_on).with(...).and_return(...)` – Andy Waite Jun 05 '15 at 10:06

1 Answers1

0

try:

allow(Post).to receive_message_chain(:on, :count) { 123 }

letz
  • 1,762
  • 1
  • 20
  • 40