4

I'm giving a try to Apotonick's Trailblazer gem, which brings more structure on top of Rails, and I really like what I tried so far, while not having embraced all of it yet. And this is one strength of Trailblazer, you can dive into it progressively, bringing it step by step into your Rails projects. I bought the Trailblazer book, which I'm following now and that leads to my question.

I'm working on the sample app ( @see https://github.com/apotonick/gemgem-trbrb ) but I'm using rspec.

I wanted to test cells output in isolation. In the book, the testing framework is Test::Unit and some helper methods ship with cells for Test::Unit.

With rspec it's another story ... I tried rspec-cells but it seems not to work with current cells version (4.0), which is used in Trailblazer. So I tried to do some salmon coding, the goal being to have the smallest setup possible for retreiving the cell's output. This led to a module with a simple helper

Here is the code (also here : https://github.com/demental/gemgem-trbrb/blob/3ec9df1d5f45b880f834486da3c150d4b65ec627/spec/support/cells.rb )

module Cell
  module Rspec
    private
    def concept(name, *args)
      controller_stub = double(
        url_options: {
          host: '',
          optional_port: 80,
          protocol: 'http',
          path_parameters: ''
        }
      )
      Capybara.string(Cell::Concept.cell name, controller_stub, *args)
    end
  end
end

RSpec.configure do |config|
  config.include Cell::Rspec, type: :cell
end

The reason why I needed to make a stubbed url_options method was for the pathHelpers methods to work in cell views, without having to setup a full controller (with a full request object).

I like it in a way that it makes a very minimalistic setup. But I'm wondering if it's not gone too dummy, as I just mimic a controller, but I am feel like I don't get rid of its dependency. What do you think ?

freemanoid
  • 14,592
  • 6
  • 54
  • 77
demental
  • 1,444
  • 13
  • 25

1 Answers1

1

I known that this post is old, but I see that you have still no resolved problem in your code.

You need to add controller to third parameter of cell method, because second parameter in cell is model and third is options. You can do it like below:

controller_stub = double(
  url_options: {
    host: '',
    optional_port: 80,
    protocol: 'http',
    path_parameters: ''
  }
)
args[1] = {} if args.length < 2
args[1].merge! controller: controller_stub
Capybara.string(Cell::Concept.cell(name, *args).to_s)

Method Cell::Concept.cell is just wrapper around ViewModel.cell, when you look at this method you can see it add controller to options parameter:

def cell(name, model=nil, options={})
  ViewModel.cell(name, model, options.merge(controller: parent_controller))
end

and going deeper parent_controller is defined in ViewModel class and initialized in constructor:

def initialize(model=nil, options={})
  @parent_controller = options[:controller]

  setup!(model, options)
end
attr_reader :parent_controller

I hope that will be helpful for you.

Michał Zalewski
  • 3,123
  • 2
  • 24
  • 37