I could be barking up the wrong tree here, but I'm trying to build a shoulda matcher for rails view specs.
Right now I have a view spec that looks something like this:
require 'rails_helper'
RSpec.describe 'layouts/_navigation', :type => :view do
context 'everyone' do
before :each do
allow(view).to receive(:user_signed_in?).and_return(false)
render
end
it 'shows links to start reading' do
expect(rendered).to have_link(I18n.t(:start_reading), :href => stories_path)
end
it 'shows links to home page' do
expect(rendered).to have_link(I18n.t(:unknown_tales), root_path)
end
end
end
What I'd like to be able to replace this with is something like this:
require 'rails_helper'
RSpec.describe 'layouts/_navigation', :type => :view do
context 'everyone' do
before :each do
allow(view).to receive(:user_signed_in?).and_return(false)
render
end
it {should have_valid_link(I18n.t(:start_reading), stories_path)}
it {should have_valid_link(I18n.t(:unknown_tales), root_path)}
end
end
I've taken a stab at this, with the following, but it doesn't work:
RSpec::Matchers.define :have_valid_link do |title, path|
description { "have valid link to #{path} on #{title}" }
match do |rendered|
expect(rendered).to have_link(title, :href => path)
end
end
I'm not sure if this doesn't work because I'm doing it wrong, or if shoulda matchers was never designed to work with views.
Thanks