How to test with Rspec?

Asked

Viewed 183 times

0

Even studying TDD with Rspec I still have difficulties understanding how to perform certain test.

How would I perform a test for this method that has a return of articles?

OBS:

  1. I use Mongodb with the mongoID ORM.
  2. In this case, it is really necessary to carry out the test?
class HomeController < ApplicationController
   def index
      @articles = Article.all
   end
end
  • 2

    It depends. What you waiting that this method returns? What is an article within your system?

2 answers

1

You can make a test that created a particular article in the database, the return should NOT be null or the reverse. There are several ways to test this.

I suggest you take a read in this post.

It’ll help you a lot.

0

Before creating the test, if you use TDD (Test Driven Development), you have to know what your implementation, in case your controller’s action index, will return.

In your case, it returns an array with all the articles, so you can create an assigns that will simulate your instance variable:

To create the articles in the development environment, use a before each, preferably with some type Factory Girl:

Rspec.describe ArticlesController do
  before :each do
    @article1 = Article.create(name: 'foo')
    @article2 = Article.create(name: 'bar')
  end

  describe '#index' do
    it 'return an array of articles' do
      assigns(:articles).should eq([@article1, @article2]).
    end
  end
end

Browser other questions tagged

You are not signed in. Login or sign up in order to post.