我试图在RSpec中测试一个Rails控制器关注点(虽然我不确定这个事实是否与我的问题有关),所以我想我应该使用一个匿名控制器。这是我的装置:
describe MyConcern do
controller do
include MyConcern
def edit
puts "Checkpoint!"
@value = concern_method
render text: "test edit method called"
end
end
it "should call c
我正在尝试设置VsCode以在调试模式下运行规范测试。
我已将以下launch.json文件添加到项目中。我正在使用rvm,并已在该位置安装了必要的宝石,如rspec。
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"ver
在我的一个控制器规范中得到了非常奇怪的rspec行为。
最好是举例说明。在rubymine中,当我设置断点时,会发生这种情况:
#rspec test
describe Api::V1::UsersController do
let(:user) { FactoryGirl.create(:user) }
describe "#show" do
it "responds successfully" do
get 'show', id: user.id
response.should be_success
我想在调试期间在Rails日志中显示每个示例的描述。我不太明白这是怎么做到的。我看过使用RSpec.configure来做类似这样的事情:
RSpec.configure do |config|
config.before(:each) do |example|
Rails.logger.debug example.description
end
end
但这只给出了一个NoMethodError,因为它无法在这一点上找到描述。我认为它可能会像#around那样工作,但我猜不是。
更新:这实际上很好。请忽略这个问题
我习惯于在byebug控制器测试中调用RSpec,但这在RSpec 中没有任何作用.它不会产生错误,但也不会产生任何影响。是否有其他方法可以在请求规范中插入调试器?
例如:
# spec/controllers/users_controller_spec.rb
require 'rails_helper'
describe UsersController do
describe "GET #index logged out" do
it "returns 401 when not logged in"
有没有人知道如何在rspec中使用gem来测试gem的控制器?我尝试过和,但没有成功。
我在gem中有一个控制器,如下所示:
module mygem
class PostsController < ::ApplicationController
def index
@posts = Posts.find(:all)
@other_var = 10
end
end
end
我希望在我的应用程序中有一个像spec/controller/post_controller_spec.rb这样的测试
describe PostsController do
我有这个(通过)控制器测试: RSpec.describe UsersController, type: :controller do
...
it do
patch :update, id: user.id.to_s, user: params
expect(response).to be_redirect
expect(assigns[:user]).to eq user
end 我想用调试器调试控制器,所以我在控制器的开头添加了byebug def update
byebug
Rails 3、RSpec 2:
在控制器的rspec测试中,我使用post和pass传入一些参数:
it "returns a job id" do
post :new, { :a => 'b'}
response.status.should == 200
end
这在控制器params[:a] is 'b'中运行良好。
但是,控制器端的request.url是
我不希望在URL中包含参数,因为在实际场景中(外部测试),URL是安全签名的一部分。
我的问题是,为什么rspec post会将参数添加到URL中,而不是像浏览器中的PO
我遵循了,当我试图运行保护程序时,它会给出以下错误:
Guard is now watching at '/Users/m/work/'
Starting Spork for Test::Unit & RSpec
Couldn't find a supported test framework that begins with 'testunit'
Supported test frameworks:
( ) Cucumber
(*) RSpec
Legend: ( ) - not detected in project (*) - d
tl;dr:无法rspec-测试设备控制器,请帮助。
我正在使用Devise,并具有以下路由(默认devise_for路由):
GET /users/cancel(.:format) devise/registrations#cancel
POST /users(.:format) devise/registrations#create
GET /users/sign_up(.:format) devise/registrations#new
GET /users/edit(.:format) devise/regi
在Rails应用程序中,我正在编写Rspec测试用例。 我有一个控制器文件,它有一个名为'index‘的方法,它将一些值返回给视图文件,该文件用作该特定视图文件中某个表的头值。代码示例如下所示。 def index @some_variable = [I18n.t('controllerFileName.index.someTitle1'), I18n.t('controllerFileName.index.someTitle2')] end sometitle1和sometitle2将作为视图文件的头文件。
我正在尝试编写一个控制器测试,它测试一个子域约束。但是,如果子域不准确,我无法让RSpec设置子域并返回错误。
我使用的是Rails 4.2.6和RSpec ~3.4
routes.rb
namespace :frontend_api do
constraints subdomain: 'frontend-api' do
resources :events, only: [:index]
end
end
events_controller.rb
module FrontendAPI
class EventsController < FrontendAP
我正在编写控制器的update方法的Rspec,它用给定的id ID更新表T中的记录。在spec中,我想添加一个用例来检查表T中是否存在任何带有ID的记录,如果不存在,它应该会给出类似以下的错误,但作为RSpec的新手,我无法完成它。
it "should give error when record with the given id does not exist" do
pending "case yet to be handled"
end
所以如果有人帮我写这个规范的话。
我有控制器:
class AccountController < ApplicationController
def index
end
private
def current_account
@current_account ||= current_user.account
end
end
如何使用rspec测试私有方法current_account?
附言:我使用Rspec2和Ruby on Rails 3
我的应用程序有一个控制器规范,它测试控制器上的create方法。create操作实际上运行良好,但是规范失败了。它似乎是自动将散列POST参数转换为字符串。
let(:coupon) { attributes_for(:coupon) }
describe 'POST #create' do
it 'should create a new coupon from params' do
expect {
post :create, :coupon => coupon
}.to change(Coupon, :count).b