【发布时间】:2018-06-01 05:02:46
【问题描述】:
硒网络驱动程序
当我尝试进行集成测试时,我的断言失败了:
@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)
【问题讨论】:
-
您在 Capybara 上使用什么驱动程序?您使用的是什么 Capybara 配置设置?在点击链接之前你在做什么?还要显示与您的错误相关的堆栈跟踪。见-stackoverflow.com/help/how-to-ask。奇怪的是,由于不需要“capybara/rails”,您的 POST 链接被解释为 GET,或者您的 JS 中的错误阻止 UJS 代码初始化。不过,如果没有更多信息,就不可能说出来。
-
添加了更多信息
-
如果您实际使用
selenium-webdriver进行此测试(它正在打开 firefox/chrome 浏览器窗口等),那么您的 JS 中很可能有错误,导致 rails ujs 无法正确初始化,并且因此 click_link 生成的是 GET 而不是 POST。检查您的 test.log 以验证发出的请求类型。 -
我的 js 确实有错误,但纠正它并不能解决问题。实际上我使用黄瓜做测试,所以没有打开浏览器选项卡,这一切都在后台。在哪里可以找到日志文件?
-
好的,我解决了。重定向代码位于未通过的 if 语句中。谢谢您的帮助。
标签: ruby-on-rails ruby rspec cucumber capybara