开发者

How to test rspec ActiveRecord queries with multiple assocations

I have several methods that query the database if the right paramater is passed in, and am trying to find the best way to test them.

At this point I'm using stub_chain's but feel this is tied very closely to implementation, and if I change my search it will break the test. This is the query:

def query 
   self.users.active.find_by_name("john") 
end

and i test it like this:

client.stub_chain(:users, :active, find_by_name).and_return([mock_model("User")])
client.query.should_not be_blank

This works, as well as just calling query without stubbing and checking to see if the return is either an array or an empty array, but neither of those seem optimal. As it is now I'm basically just testing whether I make this exact开发者_StackOverflow call or not.


If you want to untie a bit, you'd better stubbing the query method itself:

client.stub(:query).and_return [mock_model("User")]

Otherwise, it's fine.


Bonus if you want to test exact method calls, look here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜