Ruby on rails 调试RSepec的困难

Ruby on rails 调试RSepec的困难,ruby-on-rails,rspec,capybara,Ruby On Rails,Rspec,Capybara,我正试图在RSpec中调试我的特性规范。但是我不能得到一个例外。如果我在auth.save之前放置一个binding.pry,我可以破门而入。然后,我检查是否auth.valid?并返回true。我还手动调用auth.save,没有抛出异常。但是,如果我在user.update\u for\u omniauth omniauth之后放置一个窥探器,它不会被击中。实际上没有调用来自\u omniauth的方法update\u,因为我正在存根它。在我的方法的末尾有一个救援块。在那里放置撬棍也不会触发

我正试图在RSpec中调试我的特性规范。但是我不能得到一个例外。如果我在
auth.save之前放置一个
binding.pry
,我可以破门而入。然后,我检查是否
auth.valid?
并返回true。我还手动调用
auth.save
,没有抛出异常。但是,如果我在
user.update\u for\u omniauth omniauth
之后放置一个窥探器,它不会被击中。实际上没有调用来自\u omniauth的方法
update\u,因为我正在存根它。在我的方法的末尾有一个救援块。在那里放置撬棍也不会触发任何东西。我的规范失败,因为我在数据库中找不到身份验证或用户

身份验证控制器 规范版本1 规范版本2 更多规格代码
测试失败的最可能原因是,当
单击链接
返回时,由
单击链接
触发的操作不能保证已完成。您可以通过在
单击链接
后添加几秒钟的
睡眠
来测试这一点。如果这解决了您的测试问题,那么您需要将
睡眠
替换为页面上的视觉变化

click_link 'Login via Facebook'
expect(page).to have_text('You have logged in!') # expectation for whatever happens on the page after a successful login
expect ...  # rest of your test.

注意:在功能测试中对用户使用模拟/存根方法通常是一个坏主意(在功能测试中模拟除外部服务以外的任何东西通常是一种坏代码味道),从功能测试直接访问数据库也是一个坏主意。功能测试旨在测试用户在站点上的体验,而不是直接测试实现细节。

您的测试目标是什么?当您的登录出现异常时,是否要测试rescue块?@imechemi测试的目标是验证在不同场景下创建用户的角色。我还没弄明白这个逻辑。只是想让测试先开始。
   it 'set organically login user as propertyuser' do
      visit '/users/sign_in'
      click_link 'Login via Facebook'
      expect(Authentication.last.uid).to eq('654321')
    end
    it 'set organically login user as propertyuser' do
      visit '/users/sign_in'
      click_link 'Login via Facebook'
      expect(User.last.email).to eq('facebookuser@mail.com')
    end
    before do
      setup_omniauth
      application_controller_patch
      allow(Facebook).to receive_message_chain(:oauth_for_app, :exchange_access_token_info).and_return('access_token' => '123', 'expires' => 500_000)
      allow_any_instance_of(Authentication).to receive(:refresh_facebook_token) #.and_return(true)
      allow_any_instance_of(User).to receive(:update_facebook_properties)# .and_return(true)
      allow_any_instance_of(User).to receive(:update_for_omniauth)# .and_return(true)
    end

  def setup_omniauth
    OmniAuth.config.test_mode = true
    OmniAuth.config.mock_auth[:facebook_app_rewards] = OmniAuth::AuthHash.new(
      'provider' => 'facebook',
      'uid' => '654321',
      'info' => {
        'first_name' => 'Facebook',
        'last_name' => 'User',
        'email' => 'facebookuser@mail.com',
        'image' => 'https://randomuser.me/api/portraits/med/men/65.jpg'
      },
      'credentials' => {
        'token' => '123456',
        'secret' => 'top_secret',
        'expires_at' => 2.days.from_now
      }
    )
    Rails.application.env_config['devise.mapping'] = Devise.mappings[:user]
    Rails.application.env_config['omniauth.auth'] = OmniAuth.config.mock_auth[:facebook_app_rewards]
  end

  def application_controller_patch
    ApplicationController.class_eval do
      def omniauth
        Rails.application.env_config['omniauth.auth']
      end
    end
  end
click_link 'Login via Facebook'
expect(page).to have_text('You have logged in!') # expectation for whatever happens on the page after a successful login
expect ...  # rest of your test.