Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Devise view specs

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.

like image 777
janosrusiczki Avatar asked Dec 11 '25 18:12

janosrusiczki


1 Answers

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
like image 173
mbuechmann Avatar answered Dec 14 '25 06:12

mbuechmann