Reputation: 6036
I have the following helper:
def devise_error_messages!
return "" if resource.errors.empty?
messages = resource.errors.full_messages.map { |msg| content_tag(:li, msg) }.join
sentence = I18n.t("errors.messages.not_saved",
:count => resource.errors.count,
:resource => resource.class.model_name.human.downcase)
html = <<-HTML
<div id="error_explanation" class="alert alert-danger">
<h2>#{sentence}</h2>
<ul>#{messages}</ul>
</div>
</br>
HTML
html.html_safe
end
I want to write a spec for it using rspec and capybara. How should I start ? Any help would be appreciated.
Thanks in advance.
Upvotes: 0
Views: 779
Reputation: 695
I tried that but it gave me errors so I modified it like this
require 'spec_helper'
RSpec.describe DeviseHelper, type: :helper do
let(:resource_name) { (:contact) }
let(:devise_mapping){ (Devise.mappings[:contact]) }
context 'no error messages' do
let(:resource) { (Contact.new) }
it '#devise_error_messages!' do
expect(devise_error_messages!).to eq("")
end
end
context 'with error messages' do
let(:resource) { (Contact.create) }
it '#devise_error_messages!' do
expect(devise_error_messages!).to_not eq("")
end
end
end
Upvotes: 0
Reputation: 4713
Here's an update with the new rspec syntax:
require 'rails_helper'
RSpec.describe DeviseHelper, type: :helper do
before do
allow(view).to receive(:resource).and_return(User.new)
allow(view).to receive(:resource_name).and_return(:user)
allow(view).to receive(:devise_mapping).and_return(Devise.mappings[:user])
end
context 'no error messages' do
it '#devise_error_messages!' do
expect(helper.devise_error_messages!).to eq("")
end
end
context 'with error messages' do
it '#devise_error_messages!' do
allow(view).to receive(:resource).and_return(User.create)
expect(helper.devise_error_messages!).to_not eq("")
end
end
end
Upvotes: 1
Reputation: 6036
I figured it out by myself:
require 'spec_helper'
describe DeviseHelper do
before do
view.stub(:resource).and_return(User.new)
view.stub(:resource_name).and_return(:user)
view.stub(:devise_mapping).and_return(Devise.mappings[:user])
end
describe "No Error Message" do
it { helper.devise_error_messages!.should eql("") }
end
describe "Error Message Present" do
it {
view.stub(:resource).and_return(User.create)
helper.devise_error_messages!.should_not eql("")
}
end
end
Upvotes: 1