Tag: rspec rails

Rspec极其缓慢

我的rspec测试似乎运行速度非常慢,即使有防护和spork。 Finished in 5.36 seconds 13 examples, 2 failures 我知道我可以做几件事来优化我的测试并减少与数据库的交互,但我强烈怀疑spec_helper设置不正确。 我在轨道3.2.11与mongoid。 每次运行后数据库清理程序都会清理。 spec_helper.rb require ‘rubygems’ require ‘spork’ Spork.prefork do ENV[“RAILS_ENV”] ||= ‘test’ require File.expand_path(“../../config/environment”, __FILE__) require ‘rspec/rails’ require ‘rspec/autorun’ require ‘capybara/rspec’ Dir[Rails.root.join(“spec/support/**/*.rb”)].each {|f| require f} DatabaseCleaner[:mongoid].strategy = :truncation RSpec.configure do |config| config.infer_base_class_for_anonymous_controllers = false config.order = “random” config.filter_run focus: true config.filter_run_excluding :remove => true config.run_all_when_everything_filtered […]

黄瓜和Rspec

任何人都可以建议我黄瓜和rspec教程(轨道3)的良好来源(简单的例子)??? 编辑: 其实我正在寻找免费的在线资源和良好的例子..

使用Rspec测试“Post create”

我正在尝试用Rspec测试“Post create”动作。 代码如下: def valid_attributes { :zone => Flymgr::Zone.new(:countries => Flymgr::ZoneCountry.first, :name => ‘USA’, :description => ‘USA Flight’, :zipcodes => ”), :price => ‘100.00’, :class => ‘first’, } end def valid_session {} end before(:each) do @request.env[“devise.mapping”] = Devise.mappings[:admin] admin = FactoryGirl.create(:admin) sign_in admin end describe “POST create” do describe “with valid params” do it “creates […]

Factory Girl / Capybara从数据库测试中删除记录?

与RSpec和Capybara合作,我得到了一个有趣的测试失败模式,在测试用例中有一些细微的重排线……这些都不重要。 我正在开发自己的身份validation系统。 它目前正在工作,我可以登录/退出浏览器和会话工作等等。但是,尝试测试这是失败的。 正在发生的事情我不太明白,这似乎取决于(看似)不相关的电话的顺序。 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 […]

为什么这个rspec请求规范没有更新模型?

我有与User模型交互的请求规范。 我想确保具有Admin角色的用户可以创建/编辑/销毁用户。 我现在遇到问题,编辑操作不会更新用户。 当我手动浏览网站上的操作时,一切正常,但测试无法更新用户。 这是我的规格: it ‘edits a user’ do @user = FactoryGirl.create(:user) visit new_user_session_path unless current_path == new_user_session_path fill_in “Email”, :with => @user.email fill_in “Password”, :with => @user.password click_button “Sign In” user_to_edit = FactoryGirl.create(:user, first_name: “John”, last_name: “Smith”) visit edit_user_path(user_to_edit) unless current_path == edit_user_path(user_to_edit) fill_in ‘user_last_name’, with: “Changed” expect{ click_button “Do it” }.to change […]

Rspec:PG :: ConnectionBad:PQsocket()无法获取套接字描述符

我运行我的rspec,大部分测试都失败了。 我得到了同样的错误,这是: Failure/Error: Unable to find matching line from backtrace ActiveRecord::StatementInvalid: PG::ConnectionBad: PQsocket() can’t get socket descriptor: BEGIN 我发现了一个类似于我的问题的问题,但是还没有答案,我也尝试过这个链接的解决方案,但它对我没有任何影响。 我打开我的测试控制台并运行一些最简单的查询,它工作正常。

通过RSpec发送自定义标头

鉴于我的API消费者需要发送客户HTTP标头,如下所示: # curl -H ‘X-SomeHeader: 123’ http://127.0.0.1:3000/api/api_call.json 然后我可以在before_filter方法中读取此标头,如下所示: # app/controllers/api_controller.rb class ApiController < ApplicationController before_filter :log_request private def log_request logger.debug "Header: #{request.env['HTTP_X_SOMEHEADER']}" … end end 到目前为止很棒。 现在我想使用RSpec测试这个,因为行为有变化: # spec/controllers/api_controller_spec.rb describe ApiController do it “should process the header” do @request.env[‘HTTP_X_SOMEHEADER’] = ‘123’ get :api_call … end end 但是,在ApiController中收到的request将无法找到标头变量。 使用HTTP_ACCEPT_LANGUAGE标头尝试same code时,它将起作用。 自定义标头是否在某处过滤? PS:网络上的一些示例使用request而不是@request 。 虽然我不确定当前Rails 3.2 / […]

控制器规范中的RSpec存根辅助方法

发现类似的问题,但令人惊讶的是,我发现,给出一个简单的答案…… 试图在我的控制器规范中存根一个辅助方法; 不太确定哪个对象需要加倍? Controller调用此方法: #app/helpers/sessions_helper.rb def signed_in? current_user.present? end 我想在规范中将其存根以返回true / false。

Oauth模型关注测试覆盖存根

我试图找出在这个课程上达到100%测试覆盖率的最佳方法。 我概述了我的完整规范,我希望有人可以指出我正确的方向。 我的假设是将Oauth2请求存根,但我似乎无法做到这一点。 我正在使用Rails 4。 规格 RSpec.describe ‘AppOmniAuthentication’, type: :concern do let(:klass) { User } let(:user) { create(:user) } let(:user_oauth_json_response) do unfiltered_oauth_packet = load_json_fixture(‘app_omni_authentication_spec’) unfiltered_oauth_packet[‘provider’] = unfiltered_oauth_packet[‘provider’].to_sym unfiltered_oauth_packet[‘uid’] = unfiltered_oauth_packet[‘uid’].to_i unfiltered_oauth_packet end before do OmniAuth.config.test_mode = true OmniAuth.config.mock_auth[:app] = OmniAuth::AuthHash.new( user_oauth_json_response, credentials: { token: ENV[‘APP_CLIENT_ID’], secret: ENV[‘APP_CLIENT_SECRET’] } ) end describe ‘#from_omniauth’ do let(:app_oauth) { […]

如何使用page.find使用rspec测试查找变量

我有一个测试用例,我的大多数其他页面至少有一个只是直文的字段,可以使用以下方法找到: page.find(“tr”, text: “What I filled in”).find(“a.tick”).click 此页面的所有选项都是下拉选项,那么如何找到变量呢? 其余语法如下所示: it “edits person job and redirects to index” do expect(p = FactoryGirl.create(:person)).to be_valid() expect(j = FactoryGirl.create(:job)).to be_valid() visit new_job_path select p.name, from: “person_job_person_id” select p.name, from: “person_job_job_id” click_button “create person job” page.find(“tr”, p).find(“a.tick”).click end 它无法找到要点击的p?