访问具有不同格式的页面的最佳方式(黄瓜)

时间:2011-03-22 01:15:29

标签: ruby-on-rails ruby cucumber

我正在尝试做这样的事情:

Scenario: Reading feeds via json
    When I go to the feeds page using json
    Then I should see a json file
    And I should see 3 feeds in the json file

但是我不确定如何在步骤中获得“当我使用json转到feed页面”时得到feeds_path(:format =>“json”)。

这样做的最佳方式是什么?

4 个答案:

答案 0 :(得分:1)

可以灵活地使用任何格式(或缺少)的解决方案:

在你的paths.rb文件中:

def path_to(page_name)
    # Split out format if page_name includes ' using '
    # Example: When I go to the accounts page using json
    page_name, format = page_name.split(' using ')
    case page_name

    when /the home\s?page/
      '/'
    when /the new account page/
      #pass format
      new_account_path(:format => format)

    else
      begin
        page_name =~ /the (.*) page/
        path_components = $1.split(/\s+/)
        # Also make sure to pass format to the 'guessed' path
        self.send(path_components.push('path').join('_').to_sym, :format => format)
      rescue Object => e
        raise "Can't find mapping from \"#{page_name}\" to a path.\n" +
          "Now, go and add a mapping in #{__FILE__}"
      end
    end
  end
end

答案 1 :(得分:0)

试试这个:

When %r{^I go to the (\w+) page using (\w+)$} do |page, format|
  path = self.send([page, 'path'].join('_').to_sym, :format => format)
  visit path
end

答案 2 :(得分:0)

When /^I go to the feeds page using json$/ do
  visit feeds_path(:format => "json")
end

答案 3 :(得分:0)

你可以在你的paths.rb文件中尝试这样的事情:

  def path_to(page_name)
    case page_name
    when /^the feeds page( using (.*?))?$/
      format, uri = $2, '/feeds'
      format.blank? ? uri : "#{uri}.#{format}"
    end
  end

[增订]