Controller testing with Rspec rails

24 views
Skip to first unread message

ruby rails

unread,
Apr 29, 2014, 7:58:17 AM4/29/14
to rubyonra...@googlegroups.com
Hi,

I have written some controller methods and testing it using Rspec. I am
just learning the Rspec by testing those methods. I need some hints on
how to do it. I have the below method in my QueueItemsController and I
need to test it. So how do I proceed. Please help. I read the tutorials
and other online forums but it seems to be confusing at some point.

def next
@receiver = @queue.pop()
unless @receiver.nil?
respond_with(@receiver)
else
render :json =>{"msg"=>"No more Items in the Queue to
retrieve..!!"}
end
end

--
Posted via http://www.ruby-forum.com/.

Frederick Cheung

unread,
Apr 30, 2014, 2:42:41 AM4/30/14
to rubyonra...@googlegroups.com


On Tuesday, April 29, 2014 12:58:17 PM UTC+1, Ruby-Forum.com User wrote:
Hi,

I have written some controller methods and testing it using Rspec. I am
just learning the Rspec by testing those methods. I need some hints on
how to do it. I have the below method in my QueueItemsController and I
need to test it. So how do I proceed. Please help. I read the tutorials
and other online forums but it seems to be confusing at some point.

In  a nutshell you think of the possible case, what the outcome should be and how do you want to test that the outcome was as expected.

For example, one of those cases could be

context 'the queue is empty' do
  before(:each) do
    #some setup so that @queue.pop will return nil
  end
  it 'should render a json message' # change this title to be something descriptive
    get :next #add params if applicable
    ActiveSupport::JSON.parse(response.body).should == {"msg"=>"No more Items in the Queue to 
retrieve..!!"} 
  end
end

Fred
Reply all
Reply to author
Forward
0 new messages