Rails BDD无法找到可见的xpath“/ html”(Capybara :: ElementNotFound)

时间:2017-12-18 19:52:41

标签: ruby-on-rails ruby rspec cucumber capybara

硒的webdriver

当我尝试进行集成测试时,我在这个断言中失败了:

@user = FactoryBot.create(:user)
@client = FactoryBot.create(:client, user: @user)
@event = FactoryBot.create(:event, client: @client)
visit client_path(@client)
click_link "Bill"

并收到此错误

 And an invoice is created to the client                  # features/step_definitions/new_invoice_steps.rb:9
  Unable to find visible xpath "/html" (Capybara::ElementNotFound)
  ./features/step_definitions/new_invoice_steps.rb:12:in `"an invoice is created to the client"'
  features/new_invoice.feature:15:in `And an invoice is created to the client'

这是链接代码

<%= link_to "Bill", bill_client_path(@client), method: :post%>

这是我在客户端控制器中调用的方法

def bill

#some code here

  respond_to do |format|
    if invoice.save
      flash[:success] = 'Invoice was successfully created.'
      format.html { redirect_to invoice}
      format.json { render :show, status: :created, location: invoice }
    else
      format.html { render :new }
      format.json { render json: invoice.errors, status: :unprocessable_entity }
    end
  end
  end

当我在链接后停止点击byebug并输入current_url它给了我这个

"http://www.example.com/clients/1/bill"

没有视图,因为bill方法旨在重定向到发票视图。 Capybara卡在那个页面上,当然给了我一个空白页面。 为什么Capybara没有获得第二次重定向? 我怎么解决这个问题?

测试日志

    Started POST "/clients/1/bill" for 127.0.0.1 at 2017-12-18 15:54:55 +0100
Processing by ClientsController#bill as HTML
  Parameters: {"id"=>"1"}
  [1m[36mUser Load (0.7ms)[0m  [1m[34mSELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?[0m  [["id", 1], ["LIMIT", 1]]
  [1m[36mClient Load (0.9ms)[0m  [1m[34mSELECT  "clients".* FROM "clients" WHERE "clients"."user_id" = ? AND "clients"."id" = ? LIMIT ?[0m  [["user_id", 1], ["id", 1], ["LIMIT", 1]]
  [1m[36mEvent Exists (0.4ms)[0m  [1m[34mSELECT  1 AS one FROM "events" WHERE "events"."client_id" = ? AND "events"."billed" IS NULL LIMIT ?[0m  [["client_id", 1], ["LIMIT", 1]]
No template found for ClientsController#bill, rendering head :no_content
Completed 204 No Content in 210ms (ActiveRecord: 2.0ms)

1 个答案:

答案 0 :(得分:0)

您将需要DatabaseCleaner gem,因为事务夹具不能与Selenium一起使用。如果您使用框架Ruby on Rails将database_cleaner gem添加到Gemfile的:test组,并将以下代码放入spec / rails_helper.rb

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

  # 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 = false

  # Use transactions by default
  config.before do
    DatabaseCleaner.strategy = :transaction
  end

  # For the javascript-enabled tests, switch to truncation
  config.before :each, driver: :selenium do
    DatabaseCleaner.strategy = :truncation
  end

  config.before do
    DatabaseCleaner.start
  end

  config.after :each, driver: :selenium do
    load "#{Rails.root}/db/seeds.rb"
  end

  config.after do
    DatabaseCleaner.clean
  end
end

在spec文件中使用&#39;描述&#39;与特定的驱动程序:

describe 'something', type: :feature, driver: :selenium do
  ...
end