Rails Viewcomponents allow you to test if a component has rendered in minitest using
refute_component_rendered
but how do you do the same in RSpec?
class SometimesNotRenderedComponent < ViewComponent::Base
def initialize(my_param)
@my_param = my_param
end
def render?
# test this
end
end
it "renders nothing when..." do
render_inline(described_class.new(my_param))
# expect(page).to ... have no content
end
Let's dig a little, otherwise, the answer would be quite short.
The code for
refute_component_rendered
is pretty simple:https://github.com/ViewComponent/view_component/blob/v2.78.0/lib/view_component/test_helpers.rb#L14
assert_no_selector is a Capybara matcher. Negative matchers for rspec are defined dynamically with a prefix
have_no_
and are not documented.have_selector
delegates to assert_selector.https://www.rubydoc.info/gems/capybara/Capybara/RSpecMatchers#have_selector-instance_method
Which means
have_no_selector
delegates to assert_no_selector.You can use either one, it's just a matter of preference:
I've double checked: