Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何通过设计和水豚签署?_Ruby On Rails_Rspec_Devise_Capybara - Fatal编程技术网

Ruby on rails 如何通过设计和水豚签署?

Ruby on rails 如何通过设计和水豚签署?,ruby-on-rails,rspec,devise,capybara,Ruby On Rails,Rspec,Devise,Capybara,我正在使用Rspec、水豚和Desive。我需要能够登录 我的测试: describe "POST #create" do context "with valid params" do it "creates a new Poll" do @user = FactoryGirl.create(:user) visit new_user_session_path fill_in "user_email", :with => @user.em

我正在使用Rspec、水豚和Desive。我需要能够登录

我的测试:

describe "POST #create" do
  context "with valid params" do
    it "creates a new Poll" do
      @user = FactoryGirl.create(:user) 
      visit new_user_session_path

      fill_in "user_email", :with => @user.email
      fill_in "user_password", :with => "qwerty"
      click_button "commitSignIn"    

      visit '/'
      expect(page).to have_selector('.signin_username')      # OK

      binding.pry      
    end
  end
end
在Pry控制台中,我试图输出
当前用户

[1] pry(#<RSpec::ExampleGroups::PollsController::POSTCreate::WithValidParams>)> put current_user
NameError: undefined local variable or method `current_user' for #<RSpec::ExampleGroups::PollsController::POSTCreate::WithValidParams:0x00000008011c08>
from /home/kalinin/.rvm/gems/ruby-2.0.0-p598/gems/rspec-expectations-3.3.0/lib/rspec/matchers.rb:966:in `method_missing'

我将我的移动到一个
共享的上下文中

shared_context 'login' do
  def log_in_with_user(user, options={})
    email = options[:email] || user.email
    password = options[:password] || user.password
    # Do login with new, valid user account
    visit new_user_session_path
    fill_in "user_email", with: email
    fill_in "user_password", with: password, exact: true
    click_button "Log In"
  end
end
然后在测试中,您可以执行以下操作:

describe "POST #create" do
  include_context 'login'
  let(:current_user) { FactoryGirl.create(:user) }

  before do
    log_in_with_user current_user
  end

  context "with valid params" do
    it "creates a new Poll" do

      visit '/'
      expect(page).to have_selector('.signin_username')      # OK

      binding.pry      
    end
  end
end

但我的其他测试使用了“当前用户”,您需要将该用户分配给
当前用户
。designe方法
current\u user
只会在服务器端使用,但是使用Capybara,您应该考虑在客户端工作。在控制器测试中,您仍然在服务器端工作,请求测试和集成测试(Capybara)通常作为客户端完成。
describe "POST #create" do
  include_context 'login'
  let(:current_user) { FactoryGirl.create(:user) }

  before do
    log_in_with_user current_user
  end

  context "with valid params" do
    it "creates a new Poll" do

      visit '/'
      expect(page).to have_selector('.signin_username')      # OK

      binding.pry      
    end
  end
end