3

I'm used to writing view specs which check at least for something like:

expect(view).to receive(:title).with('Required page title here')

(title is a helper method I wrote to set the page title.) Now I'm trying to write specs for my Devise views which look something like:

- title 'Lost Password'
.row
  .col-lg-6
    = form_for resource, as: resource_name, url: password_path(resource_name) do |f|
      = render 'layouts/errors', object: resource
      .form-group
        = f.label :email
        = f.text_field :email, class: 'form-control', autofocus: true
      = f.submit 'Send me the instructions', class: 'btn btn-primary'
    %hr
    = render 'devise/shared/links'
  .col-lg-6
    = render 'devise/shared/advantages'

resource and resource_name are defined by Devise.

If I run the following spec on the view:

require 'rails_helper'
describe 'devise/passwords/new', type: :view do
  it 'sets the page title' do
    expect(view).to receive(:title).with('Lost Password')
    render
  end
end

It says: undefined local variable or method 'resource'. I tried:

allow(view).to receive(:resource).and_return(User.new)

but then I get [snip, long class definition] does not implement: resource.

How do I make this work? I don't want to use Capybara for something as trivial as this.

janosrusiczki
  • 1,920
  • 2
  • 20
  • 41
  • Are you implementing your devise views in the standard out-of-the-box fashion? Or are you using custom views? https://stackoverflow.com/questions/4081744/devise-form-within-a-different-controller – James Milani Mar 08 '18 at 18:33
  • @JamesMilani I'm using the views output by the generator, slightly modified for Bootstrap. I've updated the question with the relevant view code. – janosrusiczki Mar 08 '18 at 20:17
  • Hi @janosrusiczki, can you post the whole test ? `allow(view).to receive(:resource).and_return(User.new)` looks fine. – Simon Franzen Mar 10 '18 at 18:37
  • And I didnt get it with the `title`. I cannot find it in your view sample – Simon Franzen Mar 10 '18 at 18:37
  • @SimonFranzen I edited the question with better samples. – janosrusiczki Mar 11 '18 at 20:50
  • @janosrusiczki Do you use rspec ? https://stackoverflow.com/questions/13005444/testing-views-that-use-devise-with-rspec?answertab=active#tab-top – Simon Franzen Mar 11 '18 at 21:29
  • Have you tried this ? https://github.com/plataformatec/devise/wiki/How-To:-Test-with-Capybara – Simon Franzen Mar 11 '18 at 21:30
  • @SimonFranzen I saw both links and as I mentioned in the question, I don't want to use Capybara for this. – janosrusiczki Mar 13 '18 at 12:50
  • Have you tried to include the device test helpers? `RSpec.configure do |config| config.include Devise::TestHelpers, :type => :view end` – mbuechmann Mar 14 '18 at 15:36
  • @mbuechmann yes, as a matter of fact I am using sign_in in my controller specs without problems. – janosrusiczki Mar 14 '18 at 16:23
  • 1
    I think this answer might help you: https://stackoverflow.com/questions/4081744/devise-form-within-a-different-controller – mbuechmann Mar 14 '18 at 16:27
  • @mbuechmann Yes, that worked, thank you. Funny that I saw that answer but didn't try it, thinking there must be another way or that I'm doing something wrong. Oh well... Thanks again. – janosrusiczki Mar 14 '18 at 16:34

1 Answers1

2

You can provide these helpers yourself, by writing a simple implementation in your test helpers:

def resource_name
  :user
end

def resource
  @resource ||= User.new
end

def devise_mapping
  @devise_mapping ||= Devise.mappings[:user]
end
mbuechmann
  • 5,413
  • 5
  • 27
  • 40