我使用的是ruby"2.0.0"和'rails','~
我使用脚手架生成器作为控制器。所以它也生成了我的控制器测试。特别是更新操作立即中断。测试代码
describe "PUT update" do
describe "with valid params" do
it "updates the requested product" do
product = Product.create! valid_attributes
# Assuming there are no other products in the database, this
# specifies that the Product created on the previous line
# receives the :update_attributes message with whatever params are
# submitted in the request.
expect_any_instance_of(Product).to receive(:update).with({ "product_name" => "Product 1" })
put :update, {:id => product.to_param, :product => { "product_name" => "Product 1" }}, valid_session
end
控制器代码:
# PATCH/PUT /products/1
#补丁/放置 /products/1.json
def update respond_to do | format | if@product。更新属性(产品参数)格式。html{redirect_to@product,注意:'产品已成功更新'}格式。json{head:no_content}else格式。html{呈现操作:“编辑”}格式。json{render json:@product.errors,status::unprocessable\u entity}end
所以我运行我的测试并:
1) ProductsController PUT update with valid params updates the requested product
Failure/Error: Unable to find matching line from backtrace
Exactly one instance should have received the following message(s) but didn't: update
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-mocks-3.0.2/lib/rspec/mocks/any_instance/recorder.rb:92:in `verify'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-mocks-3.0.2/lib/rspec/mocks/space.rb:69:in `block in verify_all'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-mocks-3.0.2/lib/rspec/mocks/space.rb:69:in `each_value'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-mocks-3.0.2/lib/rspec/mocks/space.rb:69:in `verify_all'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-mocks-3.0.2/lib/rspec/mocks.rb:45:in `verify'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/mocking_adapters/rspec.rb:21:in `verify_mocks_for_rspec'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/example.rb:354:in `verify_mocks'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/example.rb:345:in `run_after_example'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/example.rb:163:in `block in run'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/example.rb:210:in `call'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/example.rb:210:in `block (2 levels) in <class:Procsy>'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-rails-3.0.1/lib/rspec/rails/example/controller_example_group.rb:174:in `block (2 levels) in <module:ControllerExampleGroup>'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/example.rb:294:in `instance_exec'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/example.rb:294:in `instance_exec'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/hooks.rb:430:in `block (2 levels) in run'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/example.rb:210:in `call'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/example.rb:210:in `block (2 levels) in <class:Procsy>'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-rails-3.0.1/lib/rspec/rails/adapters.rb:68:in `block (2 levels) in <module:MinitestLifecycleAdapter>'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/example.rb:294:in `instance_exec'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/example.rb:294:in `instance_exec'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/hooks.rb:430:in `block (2 levels) in run'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/example.rb:210:in `call'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/example.rb:210:in `block (2 levels) in <class:Procsy>'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/hooks.rb:432:in `run'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/hooks.rb:485:in `run'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/example.rb:303:in `with_around_example_hooks'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/example.rb:145:in `run'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/example_group.rb:494:in `block in run_examples'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/example_group.rb:490:in `map'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/example_group.rb:490:in `run_examples'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/example_group.rb:457:in `run'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/example_group.rb:458:in `block in run'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/example_group.rb:458:in `map'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/example_group.rb:458:in `run'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/example_group.rb:458:in `block in run'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/example_group.rb:458:in `map'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/example_group.rb:458:in `run'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/runner.rb:112:in `block (2 levels) in run_specs'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/runner.rb:112:in `map'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/runner.rb:112:in `block in run_specs'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/reporter.rb:54:in `report'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/runner.rb:108:in `run_specs'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/runner.rb:86:in `run'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/runner.rb:70:in `run'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/lib/rspec/core/runner.rb:38:in `invoke'
# /Users//.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-3.0.2/exe/rspec:4:in `<top (required)>'
# /Users//.rvm/gems/ruby-2.0.0-p353/bin/rspec:23:in `load'
# /Users//.rvm/gems/ruby-2.0.0-p353/bin/rspec:23:in `<main>'
# /Users//.rvm/gems/ruby-2.0.0-p353/bin/ruby_executable_hooks:15:in `eval'
# /Users//.rvm/gems/ruby-2.0.0-p353/bin/ruby_executable_hooks:15:in `<main>'
#
# Showing full backtrace because every line was filtered out.
# See docs for RSpec::Configuration#backtrace_exclusion_patterns and
# RSpec::Configuration#backtrace_inclusion_patterns for more information.
在0.27911秒内完成(加载文件需要1.85秒)16个示例,1个失败
失败的例子:
rspec/spec/controllers/products\u controller\u spec.rb:90#ProductsController PUT update with valid params更新请求的产品
在此之后,我发布了回复,问题解决了:
一个好朋友看了一眼我的代码,他指出:
expect_any_instance_of(Product).to receive(:update).with({ "product_name" => "Product 1" })
正在尝试接收来自模型的更新。模型上没有更新方法:
class Product < ActiveRecord::Base
before_validation :normalize_name, on: :create
validates :product_name, presence: true, uniqueness: true
validates :product_cost, presence: true, :numericality => { :greater_than_or_equal_to => 1 }
protected
def normalize_name
self.product_name = self.product_name.downcase.titleize
end
end
因此,修复方法包括向测试添加:update_属性,以便调用模型,而不是像以前那样作为控制器操作。
expect_any_instance_of(Product).to receive(:update_attributes).with({ "product_name" => "Product 1" })
现在测试通过了。
我希望这有帮助。我对Rails相当陌生,因此请容忍任何不准确或错误。欢迎随时报告拼写错误或更正。
我开始学习Spring框架,在Spring之前我使用Laravel。在Laravel中,我们有各种用于生成url的助手,例如: 在Thymeleaf中是否有类似的内容,基本上我想生成一个url,它将指向控制器中的一些操作,因此如果我更改控制器的映射,所有锚定标记的url都将更改。
生成一个 Http Controller 必选参数: -name 生成的 Controller 类名 -namespace 生成的 Controller 所在命名空间 可选参数: -prefix 路由前缀,不传则为类名 -render 渲染方式,默认为json,可选:html/json/xml -rest 是否生成 RESTful 风格,默认 false -override 是否覆盖已存在的文件,
英文原文:http://emberjs.com/guides/testing/testing-controllers/ 单元测试方案和计算属性与之前单元测试基础中说明的相同,因为Ember.Controller集成自Ember.Object。 针对控制器的单元测试使用ember-qunit框架的moduleFor来做使这一切变得非常简单。 测试控制器操作 下面给出一个PostsController
spring-test模块对测试控制器@Controller提供了最原生的支持。详见14.6 "Spring MVC测试框架"一节。
本文向大家介绍Yii 框架控制器创建使用及控制器响应操作示例,包括了Yii 框架控制器创建使用及控制器响应操作示例的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了Yii 框架控制器创建使用及控制器响应操作。分享给大家供大家参考,具体如下: Yii 框架控制器创建使用 在根目录下的controllers目录下创建控制器HelloController.php: 访问地址:basic/web/i
概览 触发器配置卡片 此面板配置一些触发器的基本信息,或者进行查阅触发器触发日志等操作。 触发类型目前有两种: 数据表 微信支付回调 条件卡片 对应上面的触发类型,设置不同触发类型的参数。 动作卡片 当触发器的条件被满足,将会执行触发器中的动作。 目前有四种动作类型可选,分别如下: 邮件 微信模板消息 webhook 数据表操作 不同的触发类型对应可选的动作类型不同,每个动作类型又有可选模板变量的