gpt4 book ai didi

javascript - rails : best way to test an action called remotely?

转载 作者:行者123 更新时间:2023-11-28 02:53:21 24 4
gpt4 key购买 nike

我再次尝试 Rails,这次是第 3 版,但在为仅远程调用的操作编写测试时遇到了困难。

具体示例:

Controller

class PeopleController < ApplicationController
def index
@person = Person.new
end
def create
@person = Person.new(params[:person])
@person.save
end
end

查看(index.html.erb)

<div id="subscription">
<%= form_for(@person, :url => { :action => "create" }, :remote => true) do |f| %>
<%= f.text_field :email %>
<%= f.submit "Subscribe" %>
<% end %>
</div>

查看(create.js.erb)

<% if @person.errors.full_messages.empty? %>
$("#subscription").prepend('<p class="notice confirmation">Thanks for your subscription =)</p>');
<% else %>
$("#subscription").prepend('<p class="notice error"><%= @person.errors.full_messages.last %></p>');
<% end %>

如何测试远程表单提交?我只是想知道通知消息是否正确呈现。但如果我尝试这样做

test "create adds a new person" do
assert_difference 'Person.count' do
post :create, :people => {:email => 'test@test.com'}
end
assert_response :success
end

它会说“创建”操作缺少模板。

你们通常如何测试远程调用?

最佳答案

您可以只使用“xhr”函数而不是“post”函数吗?可以在 http://weblogs.java.net/blog/2008/01/04/testing-rails-applications 找到示例。 ,如果您搜索“xhr”。但即便如此,我还是很好奇,即使是远程调用,你不需要返回一些东西吗?即使只是一个 OK header ?

关于javascript - rails : best way to test an action called remotely?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3283244/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com