vendredi 13 novembre 2015

"expected nil to be a new Article" when testing creating new object usind RSpec and Devise

I am writing tests to test my controller using RSpec. I want to test the case when user gets to the "new" page, if user is signed in or not. But the test fails and I can't figure out why.

Here is my test:

describe "on getting /new" do
  context "if signed in" do
    before(:each) do
      sign_in FactoryGirl.build(:user)
    end

    it "assigns a new Article to @article" do
      get :new
      expect(assigns(:article)).to be_a_new(Article)
    end

    it "renders the :new template" do
      get :new
      expect(response).to render_template :new
    end
  end
end

And my factory:

FactoryGirl.define do
  factory :user do
    email "this@mail.com"
    username "main_user"
    password "password"
    password_confirmation "password"
  end
end

When I run RSpec, this is what I am getting:

Failures:

  1) ArticlesController on getting /new if signed in assigns a new Article to @article
     Failure/Error: expect(assigns(:article)).to be_a_new(Article)
       expected nil to be a new Article(id: integer, title: string, text: string, created_at: datetime, updated_at: datetime, user_id: integer, shortname: string)
     # ./spec/controllers/article_spec.rb:56:in `block (4 levels) in <top (required)>'

  2) ArticlesController on getting /new if signed in renders the :new template
     Failure/Error: expect(response).to render_template :new
       expecting <"new"> but rendering with <[]>
     # ./spec/controllers/article_spec.rb:61:in `block (4 levels) in <top (required)>'

My app should redirect user to the root page if not signed in. If I comment out authenticate method in my controller and comment out sign_in part, the test will work fine, so I am assuming that it is Devise issue.

How can I fix it?

Aucun commentaire:

Enregistrer un commentaire