Tag: rspec

Capybara和之前(:全部)在rspec

这是我的完整规范: require ‘spec_helper’ describe “Idea page”, js: true do subject { page } before(:all) do create(:idea) visit root_path click_link “Log In” end context “Single idea” do before do page.find(:xpath, ‘//*[@id=”accordion”]/div/div[1]/a’).click end it { should have_selector(‘a’,text:’Claim’) } it “should have a button for reporting the idea” it “should have a button for opening all links” describe […]

NameError:未定义的局部变量或方法`app’

我创建的控制器规范失败,并显示以下错误消息: NameError: undefined local variable or method `app’ for \ # … # spec/controllers/sessions_conroller_spec.rb require ‘spec_helper’ describe SessionsController do before do @user = User.gen! end describe “#create” do context “when sending valid email and password” do it “renders a json hash …” do post :create, email: @user.email, password: @user.password expect(last_response.status).to eq(201) end end end describe […]

如何测试使用rspec修改模型上的属性?

我想检查ActiveRecord对象上是否没有修改任何属性。 目前我这样做: prev_attr = obj.attributes < – 这将给我一个带有attr名称和attr值的Hash 然后,稍后,我再次获取属性并比较两个哈希值。 还有另一种方式吗?

如何在控制器规范中禁用before_action?

我在我的控制器规范中使用了这个: controller.class.skip_before_action 具体来说,在这种情况下: controller.class.skip_before_action:require_authorisation_to_view_materials MaterialsController: class MaterialsController < ApplicationController before_action :set_material, only: [:show, :edit, :update, :destroy] before_action :require_authorisation_to_view_materials, only: [:index, :show] def require_authorisation_to_view_materials # For Materials Page unless user_signed_in? && current_user.can_view_materials? redirect_to root_path, alert: "You are not authorised to view the Materials page." end end # GET /materials # GET /materials.json def index @materials = […]

在使用Rails 3.1进行测试期间禁用终端中的SQL日志记录? (RSPEC /黄瓜)

我刚刚将我的应用程序升级到rails 3.1,现在每当我运行测试时,我的终端都会获得大量的SQL输出。 例如: (1.0ms) TRUNCATE TABLE `users`; (0.1ms) SET FOREIGN_KEY_CHECKS = 1 . Company Load (0.3ms) SELECT `companies`.* FROM `companies` LIMIT 1 Sector Load (0.3ms) SELECT `sectors`.* FROM `sectors` WHERE `sectors`.`name` = ‘General’ LIMIT 1 (0.1ms) BEGIN (0.3ms) SELECT 1 FROM `sectors` WHERE `sectors`.`name` = BINARY ‘General 63’ LIMIT 1 SQL (0.2ms) INSERT INTO `sectors` […]

单表inheritance与Rails中的工厂女孩

我正在使用FactoryGirl和FactoryGirl制作Rails 4.0.1应用程序,但我无法让我的测试正常工作。 我正在使用单表inheritance来创建Collection < ActiveRecord::Base和VideoCollection < Collection模型。 在我的测试中使用Factory Girl时,模型似乎没有像他们应该那样实例化。 细节: 当我在浏览器中直观地检查我正在测试的视图时,它会正确显示集合。 当我在测试中为同一视图运行print page.html时,该集合不会出现在页面代码中。 如果我使用rails c test进入测试控制台并执行FactoryGirl.create(:video_collection) ,那么video集合就会插入到数据库中没问题。 代码: 我的模特: # ——in app/models/collection.rb—— class Collection < ActiveRecord::Base end # ——in app/models/video_collection.rb—— class VideoCollection < Collection end # ——in db/schema.rb—— create_table "collections", force: true do |t| t.string "type" t.datetime "created_at" t.datetime "updated_at" t.string "name" t.string "tile_image_link" end […]

Rspec + Capybara可选择更改JS驱动程序

我使用poltergeist / phantomjs作为CI,但我希望能够选择将JS驱动程序更改为selenium以观察我的本地测试运行。 理想情况下,我想为这个默认的poltergeist设置一个命令行标志,但是能够运行rspec –driver = selenium(或类似的东西) 有任何想法吗?

Rails 5 – 找不到生成器’rspec:install’

在我的rails 5.0.0应用程序中,我已将以下内容添加到我的Gemfile中: group :development, :test do gem ‘byebug’, platform: :mri gem ‘rspec-rails’, ‘~> 3.5’, ‘>= 3.5.2’ end 我运行了bundle install 。 然后gem成功安装。 然后我跑了以下: rails generate rspec:install 但我得到一个错误说: Running via Spring preloader in process 8893 Could not find generator ‘rspec:install’. Maybe you meant ‘css:assets’, ‘assets’ or ‘scaffold’ Run `rails generate –help` for more options. 在这个错误上发布了大量其他问题( Could not […]

.env没有在带有rspec的Rails中加载到测试环境中

我使用gem ‘dotenv-rails’, ‘~> 0.9.0’将环境变量加载到Rails 4.0.5应用程序中。 我有.env文件和.env.test。 所有这些都在开发中运行良好,但是当涉及到测试时,我使用rspec,它无法设置环境变量。 我在测试环境中进入了Rails控制台,我可以看到它们被设置为nil。 有关如何在测试中加载dotenv的任何提示?

RSpec:如何在静态方法上使用should_receive?

我在lib / gcm.rb中有一个模块: require “net/http” require “uri” module GCM def self.dispatch_message(reg_ids, data) url = URI.parse(GCM_URL + “/send”) @msg = { :registrationIds => reg_ids, :data => data } request = Net::HTTP::Post.new(url.path) request.content_type = ‘application/json’ request.body = @msg.to_json response = Net::HTTP.start(url.host, url.port) { |http| http.request(request) } end end 我想测试在我的一个控制器中调用dispatch_message : it “should dispatch a GCM message” do […]