所以我有个密码:
begin
@location = Location.find(params[:id])
rescue ActiveRecord::RecordNotFound
puts 'ERROR MUST HAVE BEEN RAISED IF I APPEAR'
render action: :new, status: 404
end
在这里进行了测试:
let(:invalid_request){ get :show, id: Location.count + 1 }
describe 'runtime' do
bef
当我转到一个虚假的url时:
/posts/99999999
在我的本地机器上,我看到:
ActiveRecord::RecordNotFound in Posts#show
Showing /Users/patrick/rails/my_app/app/views/posts/show.html.haml where line #1 raised:
找不到带有ID=99999的帖子
..。日志显示:
Rendered posts/show.html.haml within layouts/application (29.6ms)
Completed in 423ms
ActionVi
def show
@item = Item.find(params[:id])
rescue ActiveRecord::RecordNotFound #customer with that id cannot be found
redirect_to action: :index
end
/////////////////////////////////////////////////////////////////////////
def edit
@item = Item.find(params[:id])
rescue ActiveR
如何获得此错误的测试通行证? Rspec结果 **2) Api::V1::UsersController GET #show - a user it fails showing a user
Failure/Error:
expect do
get 'show', params: { id: 2 }
end.to raise_error(ActiveRecord::RecordNotFound)
expected ActiveRecord::RecordNotFound but nothing was raised
# ./spec/con
我是Ruby on Rails的新手(但我喜欢它),我不能准确地过滤。
Mi的目标是检索由一个用户创建的答案上的所有投票。我使用的是act_as_votable_gem
在answer.rb上,我有:
class Answer < ActiveRecord::Base
acts_as_votable
belongs_to :user
belongs_to :letter
...
end
在user.rb,我有:
class User < ActiveRecord::Base
acts_as_voter
has_many :answers, depe
我有一个简单的测试用例:
it "is removed when associated board is deleted" do
link = FactoryGirl.create(:link)
link.board.destroy
expect(Link.find(link.id)).to raise_error ActiveRecord::RecordNotFound
end
它会失败,并显示输出:
1) Link is removed when associated board is deleted
Failure/Error: expect(Link.
在我的ActiveRecord:RecordNotFound中,我有一个get '/posts/:id',我实现了一个救援'/posts/:id',但是它没有重定向到根url,而是仍然显示错误。是否有方法像自定义路由错误(not_found)一样自定义ActiveRecord错误?
not_found do
slim :not_found
end
get '/posts/:id' do
begin
@post = Post.find(params[:id])
rescue ActiveRecord::RecordNotF
post = Post.find 666
ActiveRecord::RecordNotFound: Couldn't find Post with 'id'=666
后=>零
post = "Banana"
post = Post.find 666
ActiveRecord::RecordNotFound: Couldn't find Post with 'id'=666
发布=>“香蕉”
如果查找不返回任何内容,我想确保post是nil。如果空手而归,是否有一种优雅的分配零的方法?
当我想测试一个方法引发一个错误时,它非常简单:
# There is a Widget model
# Method to test
class Example
def perform(widget_id)
the_widget = Widget.find(widget_id)
end
end
# rspec test
describe "Example" do
let(:example) {Example.new}
it "does not find the record and raises an error" do
我正在尝试建立一个类似新闻网站的网站。用户可以发布文章,这些文章将被分为不同的类别,如体育,politics...etc。
我们的想法是有一个索引控制器,它包含了home/sports/politics...方法,使url类似于xxx.com/ index /home,xxx.com/index/sports。首页将显示每一篇文章。但是,我在尝试获取数据时收到了ActiveRecord::RecordNotFound消息。
到目前为止,有3个控制器。1.索引2.用户(使用devise) 3. Post (带有post模型)
在索引控制器中:
class IndexController <
我在我的控制器规格文件中有这个
it "should raise 404" do
business = FactoryGirl.build(:business)
expect{get :edit, :id => business}.to raise_error(ActiveRecord::RecordNotFound)
end
如果我是对的,那么build不会保存到数据库中,所以业务应该不存在,我的测试应该通过,但它没有。
我也尝试了一个字符串作为"id“的值,但仍然失败。
我已经尝试过这个控制器操作:
def edit
i
我刚刚开始学习使用Rails和RSpec的TDD。这是我对控制器的第一次测试。只是简单的用JSON进行响应的RESTful UserController,所以它没有新的编辑方法。请看一看
require 'spec_helper'
describe UsersController do
let(:users) { 4.times.map { create(:user) } }
describe '#index' do
before(:each) { get :index }
it 'assigns all users to
我编写这个场景是为了测试当用户访问未发布的条目时,他们会看到一个错误页面:
Scenario: Unpublished entry
Given there is an unpublished entry
When a user visits the entry page
Then he will see an error page
步骤
Given /^there is (?:an|one) unpublished entry$/ do
@entry = create(:entry, published: false)
end
When /^a user visi
我只是在学习编程,特别是Ruby on Rails。我目前面临的问题是,对于干净的代码和避免代码气味或其他问题,我怀疑用哪种方式来处理我的问题更好。
所以基本上,我使用users_controller来检查我的jwt是否有效:我解码它,提取user_id,然后在DB中通过她的id查找用户。
def login
user = decoded_user(permitted_params[:jwt])
render json: user
rescue ActiveRecord::RecordNotFound, JWT::DecodeError
render status
请帮助我找到官方文件,并列出例外情况的类型。我写了这个动作:
def show
begin
@user = User.find(params[:id])
rescue ActiveRecord::RecordNotFound => e
logger.debug e
logger.debug "Error #{$!}"
render_404
end
end
我需要提到几种例外情况:
def show
begin
@user = User.find(params[:id])
rescue Ac