我有一个模型负责人和一个回调: after_commit :create, :send_to_SPL
我使用的是Rails-4.1.0,ruby-2.1.1,RSpec.
1)此规范未通过:
context 'callbacks' do
it 'shall call \'send_to_SPL\' after create' do
expect(lead).to receive(:send_to_SPL)
lead = Lead.create(init_hash)
p lead.new_record? # => false
end
end
Run Code Online (Sandbox Code Playgroud)
2)这个规范也没有通过:
context 'callbacks' do
it 'shall call \'send_to_SPL\' after create' do
expect(ActiveSupport::Callbacks::Callback).to receive(:build)
lead = Lead.create(init_hash)
end
end
Run Code Online (Sandbox Code Playgroud)
3)这个是通过,但我认为它不是测试after_commit回调:
context 'callbacks' do
it 'shall call \'send_to_SPL\' after create' do
expect(lead).to receive(:send_to_SPL)
lead.send(:send_to_SPL)
end
end
Run Code Online (Sandbox Code Playgroud)
在Rails中测试after_commit回调的最佳方法是什么?
product我的elixir/phoenix后端有两个控制器.第一 - API端点(pipe_through :api)和第二个控制器piping through :browser:
# router.ex
scope "/api", SecretApp.Api, as: :api do
pipe_through :api
resources "products", ProductController, only: [:create, :index]
end
scope "/", SecretApp do
pipe_through :browser # Use the default browser stack
resources "products", ProductController, only: [:new, :create, :index]
end
Run Code Online (Sandbox Code Playgroud)
ProductController处理来自elixir表单助手生成的表单的请求,并接受一些文件附件.一切都很好.这是由此操作处理的create action和params:
def create(conn, %{"product" => product_params}) do
changeset = Product.changeset(%Product{}, product_params)
case Repo.insert(changeset) do
{:ok, _product} ->
conn
|> put_flash(:info, "Product created successfully.")
|> redirect(to: product_path(conn, :index))
{:error, changeset} -> …Run Code Online (Sandbox Code Playgroud)