capybara rspec更深入的记录?

时间:2012-02-26 19:19:04

标签: rspec capybara

我在执行click_button后的某个时刻检索单例无法转储错误。所有的rspec输出告诉我的是:

1) home not logged in sign in should contain content with 'Add new charity
 Failure/Error: click_button "Install"
 TypeError:
   singleton can't be dumped
 # (eval):2:in `click_button'
 # ./spec/integration/home_spec.rb:29:in `block (4 levels) in <top (required)>'

我尝试过使用-b选项,但我没有获得任何新信息。使用我的控制器中的日志记录,我可以看到操作通过并通过重定向完成。在调用接收操作之前,它必须在某个时刻失败。因此,如果有一种方法可以更好地看到堆栈跟踪,我可以找出问题所在。

添加了home_spec.rb

如您所见,使用shopify_api gem。

require 'spec_helper'

describe "home" do

  before do
    @domain = "myshop.myshopify.com"
    @token = SecureRandom.hex(16)
    @shopify_session = ShopifyAPI::Session.new(@domain, @token)
  end

  context "not logged in" do
    it "should be at login" do
      visit "/"
      page.should have_content("Install this app in a shop to get access to its private admin data")
    end

    describe "sign in" do
      before do
        ShopifyAPI::Session.should_receive(:new).and_return(@shopify_session)

        @shopify_session.should_receive(:valid?).and_return(true)

        ShopifyAPI::Session.any_instance.should_receive(:create_permission_url).and_return("/login/finalize?shop=#{@domain}&t=#{@token}")
      end

      it "should contain content with 'Add new charity" do
        visit "/"
        fill_in "shop", with: @domain
        click_button "Install"
        page.should have_content("Add new charity")
      end
    end
  end

  context "logged in" do
    before do
      page.set_rack_session(:shopify => @shopify_session)
    end

    it "should contain content with 'Add new charity" do
      visit "/"
      page.should have_content("Add new charity")
    end
  end

end

spec_helper.rb:

# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rails' 
require 'capybara/dsl'
require "rack_session_access/capybara"

# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

Rails.application.config do
  config.middleware.use RackSessionAccess::Middleware
end

RSpec.configure do |config|
  # == Mock Framework
  #
  # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
  #
  # config.mock_with :mocha
  # config.mock_with :flexmock
  # config.mock_with :rr
  config.mock_with :rspec

  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  config.fixture_path = "#{::Rails.root}/spec/factories"

  # If you're not using ActiveRecord, or you'd prefer not to run each of your
  # examples within a transaction, remove the following line or assign false
  # instead of true.
  config.use_transactional_fixtures = true

  # If true, the base class of anonymous controllers will be inferred
  # automatically. This will be the default behavior in future versions of
  # rspec-rails.
  config.infer_base_class_for_anonymous_controllers = false

  # RSpec automatically cleans stuff out of backtraces;
  # sometimes this is annoying when trying to debug something e.g. a gem
  config.backtrace_clean_patterns = [
    /\/lib\d*\/ruby\//,
    /bin\//,
    /gems/,
    /spec\/spec_helper\.rb/,
    /lib\/rspec\/(core|expectations|matchers|mocks)/
  ]
end

1 个答案:

答案 0 :(得分:1)

您是否考虑过使用像Pry这样的东西来让这更容易?它与您的home_spec中包含的某个文件有关。

您可以在这里找到Pry:https://github.com/pry/pry

然后在您的代码中,您可以插入binding.pry,您应该能够进入执行环境并检查类似irb的会话中发生的事情。

否则,您需要发布home_spec.rb,以便我们开始理论化。