(cross-posted from Ruby Rogues Parley)
I still refer to this article on an alternative to Draper all the time. I’ve finally started moving away from Draper, but one thing I’m grappling with is unit testing my view models. With Draper, I was able to use Capybara to test that the HTML was correct:
markup = Capybara.string(decorator.icon_tag)
expect(markup).to have_css("img[alt='Task Icon']")
expect(markup).to have_css("img[width='70']")
expect(markup).to have_css("img[src='#{expected_source}']")
I was happy enough to not have true isolation because the tests were cheap to write and haven’t turned out to be brittle.
Passing in ViewContext
as a dependency then leads me to stubbing and mocking because it’s difficult to recreate. This rings warning bells about not mocking objects we don’t own.
Here’s an idea of my first pass at it: Testing PORO view models with view_context injected · GitHub
Maybe I should inject FakeViewContext objects with implementations of each method I depend upon, and assert the return value of those methods instead. What do you think?