How to perform test with RSpec?

0

Even studying TDD with RSpec I still have difficulties understanding how to perform certain tests.

How would I do to perform a test for this method that has an article return?

NOTE:

  • I use MongoDB with ORM mongoID.
  • In this case, is it really necessary to perform the test?
  • class HomeController < ApplicationController
       def index
          @articles = Article.all
       end
    end
    
        
    asked by anonymous 03.02.2014 / 12:55

    2 answers

    1

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

    I suggest you read in this post .

    It will help you a lot.

        
    03.02.2014 / 20:05
    0

    Before creating the test, if you use TDD (Test Driven Development), you have to know what your implementation, in case the action index of your controller, 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 articles in the development environment, use a before each, preferably with some type of 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
    
        
    16.05.2017 / 00:57