我们最近更新了一个应用程序到Rails 3.0.4(在线开发服务器上的3.0.5).从2.3.10到3.0.4的大多数变化都是由于过时或过时的插件和宝石,并且相对容易解决.但有一件事让我发疯:
在开发模式下,每个Web请求都会导致服务器进程分配比以前多 50-60 MB的内存.请求后不释放此内存,至少不是全部内存.在10-20个请求之后,每个Ruby实例消耗超过500 MB的RAM,而我们以前的Rails 2.3.10实例很少超过200 MB.
这使得无法运行我们的1300次测试,因为在测试结束之前,开发了4GB的RAM.它只发生在开发模式中cache_classes = false.如果我将cache_classes切换为true,Rails实例将消耗大约200MB的内存,然后保留在那里.但是,在测试期间,即使cache_classes = true,内存使用量也会增加.
我查询了ObjectSpace并发现每次请求时,大约有3500个新的Proc,最多50'000个新的Strings和3000个新的Hashes和Arrays被创建并且没有被释放.这些字符串(转储时)包含我的整个源代码,包括插件和gem,文档,源代码注释和路径名.(为什么?)
为了找到原因,这是我尝试过的:(每次更改后,我都会使用ab -n 50.)
pg, rails, aasm, will_paginate, geokit-rails3, koala, omniauth, paperclip …我有这个简单的代码,我发送http请求并读取所有响应.这是我的rails代码
open("http://stackoverflow.com/questions/ask")
Run Code Online (Sandbox Code Playgroud)
如何为这行代码编写规范.我没有选择使用mocha和webmock.我只能使用Rpsec的模拟框架.
我试图使用这个声明
OpenURI.stub!(:open_uri).should_receive(:open).with("http://stackoverflow.com/questions/ask")
Run Code Online (Sandbox Code Playgroud)
但我一直收到这个错误
RSpec::Mocks::MockExpectationError: (#<RSpec::Mocks::MessageExpectation:0xd1a7914>).open("http://stackoverflow.com/questions/ask")
expected: 1 time
received: 0 times
Run Code Online (Sandbox Code Playgroud) 我正在将我的测试升级到Rspec3(多么麻烦),删除所有'应该',但我无法解决如何在我的视图测试中升级'view.stub'.
我正在使用设计
例:
view.stub(:current_user) { nil }
render
expect(rendered).to .... etc
Run Code Online (Sandbox Code Playgroud)
这给了我一个弃用警告:
不推荐使用
stubrspec-mocks的旧:should语法而不显式启用语法.使用新:expect语法或显式启用:should.来自....
我无法弄清楚如何升级到新的"改进"语法.谢谢
我正在尝试在应用程序控制器上测试一个将用作前置过滤器的方法.为此,我在测试中设置了一个匿名控制器,并应用了之前的过滤器以确保其正常运行.
测试目前看起来像这样:
describe ApplicationController do
controller do
before_filter :authenticated
def index
end
end
describe "user authenticated" do
let(:session_id){"session_id"}
let(:user){OpenStruct.new(:email => "pythonandchips@gmail.com", :name => "Colin Gemmell")}
before do
request.cookies[:session_id] = session_id
UserSession.stub!(:find).with(session_id).and_return(user)
get :index
end
it { should assign_to(:user){user} }
end
end
Run Code Online (Sandbox Code Playgroud)
应用程序控制器是这样的:
class ApplicationController < ActionController::Base
protect_from_forgery
def authenticated
@user = nil
end
end
Run Code Online (Sandbox Code Playgroud)
我的问题是,当我运行测试时,我收到以下错误
1) ApplicationController user authenticated
Failure/Error: get :index
ActionView::MissingTemplate:
Missing template stub_resources/index with {:handlers=>[:erb, :rjs, :builder, :rhtml, :rxml, :haml], :formats=>[:html], :locale=>[:en, :en]} in view paths "#<RSpec::Rails::ViewRendering::PathSetDelegatorResolver:0x984f310>" …Run Code Online (Sandbox Code Playgroud) 与RSpec和Capybara合作,我得到了一个有趣的测试失败模式,它在测试用例中有一些细微的线条重排......这些都不重要.
我正在开发自己的身份验证系统.它目前正在工作,我可以使用浏览器和会话工作等登录/退出.但是,尝试测试这是失败的.正在发生的事情我不太明白,这似乎取决于(看似)不相关的电话的顺序.
require 'spec_helper'
describe "Sessions" do
it 'allows user to login' do
#line one
user = Factory(:user)
#For SO, this method hashes the input password and saves the record
user.password! '2468'
#line two
visit '/sessions/index'
fill_in 'Email', :with => user.email
fill_in 'Password', :with => '2468'
click_button 'Sign in'
page.should have_content('Logged in')
end
end
Run Code Online (Sandbox Code Playgroud)
原样,该测试失败......登录失败.在将'调试器'调用插入规范和控制器后,我可以看到原因:就控制器而言,用户没有插入数据库:
编辑在ApplicationController中添加
class ApplicationController < ActionController::Base
helper :all
protect_from_forgery
helper_method :user_signed_in?, :guest_user?, :current_user
def user_signed_in?
!(session[:user_id].nil? || current_user.new_record?)
end
def guest_user?
current_user.new_record?
end
def current_user …Run Code Online (Sandbox Code Playgroud) class Message
attr_reader :bundle_id, :order_id, :order_number, :event
def initialize(message)
hash = message
@bundle_id = hash[:payload][:bundle_id]
@order_id = hash[:payload][:order_id]
@order_number = hash[:payload][:order_number]
@event = hash[:concern]
end
end
Run Code Online (Sandbox Code Playgroud)
require 'spec_helper'
describe Message do
it 'should save the payload' do
payload = {:payload=>{:order_id=>138251, :order_number=>"AW116554416"}, :concern=>"order_create"}
message = FactoryGirl.build(:message, {:payload=>{:order_id=>138251, :order_number=>"AW116554416"}, :concern=>"order_create"})
message.event.should == "order_create"
end
end
Run Code Online (Sandbox Code Playgroud)
失败:
1)消息应保存有效负载
Failure/Error: message = FactoryGirl.build(:message, {:payload=>{:order_id=>138251, :order_number=>"AW116554416"}, :concern=>"order_create"})
ArgumentError:
wrong number of arguments (0 for 1)
# ./app/models/message.rb:4:in `initialize'
# ./spec/models/message_spec.rb:7:in …Run Code Online (Sandbox Code Playgroud) 我是有一些问题,宙斯+ rspec的和我找到了解决办法说,我一定要删除require 'rspec/autorun'的spec_helper.rb.
这很有效,但我想知道它的用途是rspec/autorun什么?spec_helper.rb默认情况下它会出现,但无论有没有,规格都可以使用.
我刚刚在Rails 5中开始了一个新项目(我的第一个,虽然我在Rails 4.x中有几个项目)并且我在控制器规格方面遇到了麻烦.
describe RequestsController, :type => :controller do
it "receives new request" do
post :accept_request, my_params
end
end
Run Code Online (Sandbox Code Playgroud)
返回错误:
Failure/Error: post :accept_request, my_params
ArgumentError:
wrong number of arguments (given 2, expected 1)
Run Code Online (Sandbox Code Playgroud)
据我所知,日常Rails中记录的Rails 5控制器的首选测试策略发生了转变,特别是将控制器测试转换为请求规范,但没有说明这种控制器测试基本方法的变化.
我对Rails 3.1应用程序进行了RSPEC集成测试,该应用程序需要通过发出带有JSON参数的POST请求和需要使用http_basic身份验证的移动头来测试移动客户端的API,因为请求对象在集成测试中不可用我有点卡住了
这是我到目前为止的代码
it "successfully posts scores" do
# request.env["HTTP_ACCEPT"] = "application/json" #This causes an error as request is nly available in controller tests
post "scores", :score => {:mobile_user_id => @mobile_user.id, :points => 50, :time_taken => 7275}.to_json,
:format => :json, :user_agent => 'Mobile', 'HTTP_AUTHORIZATION' => get_basic_auth
end
Run Code Online (Sandbox Code Playgroud)
发布请求无法识别我使用的是http基本身份验证,但不确定json的格式是否正确.任何帮助赞赏
get_basic_auth是一个帮助me4thod,看起来像这样
def get_basic_auth
user = 'user'
pw = 'secret'
ActionController::HttpAuthentication::Basic.encode_credentials user, pw
end
Run Code Online (Sandbox Code Playgroud)
我在我的控制器中使用了before_filter来检查看起来像这样的移动和http_basic_authentication
def authorize
logger.debug("@@@@ Authorizing request #{request.inspect}")
if mobile_device?
authenticate_or_request_with_http_basic do |username, password|
username == Mobile::Application.config.mobile_login_name && Mobile::Application.config.mobile_password
end …Run Code Online (Sandbox Code Playgroud) RSpec stub_model和mock_modelRSpec有什么区别?到目前为止,我知道存根用于防止实际方法被调用并返回预定义值,而模拟实际上是期望并且要求在接收器上调用该方法.
我也知道这些存根/模拟用于允许隔离测试,例如在控制器中而不触及模型.但我仍然对这两种方法感到困惑,究竟每种方法都使用了吗?非常感谢细节和例子.非常感谢!
rspec-rails ×10
rspec ×5
factory-bot ×2
rspec2 ×2
ruby ×2
capybara ×1
memory-leaks ×1
postgresql ×1
testing ×1