rspec-rails

FactoryGirl - Validation failed: Time zone is not included in the list

只愿长相守 提交于 2019-12-11 10:31:34
问题 I'm using Rspec, FactoryGirl and Capybara. When using ActiveSupport::TimeZone.us_zones. I run my test for my requests/users_spec and it doesn't even hit the test as it has an issue at the Factory. Here is the error: Failure/Error: make_user_and_login ActiveRecord::RecordInvalid: Validation failed: Time zone is not included in the list # ./spec/support/user_macros.rb:3:in `make_user_and_login' # ./spec/requests/users_spec.rb:7:in `block (3 levels) in <top (required)>' This is my setup: app

What is the correct usage of capybara's within method?

微笑、不失礼 提交于 2019-12-11 10:09:28
问题 For feature examples for the footer I've used this code: feature 'in footer' do scenario "has a Copyright text" do within('footer') { expect(page).to have_content "Copyright" } end scenario "has navigation bar" do within('footer') { expect(page).to have_selector 'nav ul li' } end scenario "has a link for 'About'" do within('footer') { expect(page).to have_link 'About', href: '#' } end end If you look closely I repeated the "within" in each scenario and this conflicts with dryness of code. I

How to write unit test using rspec for serializer with custom attributes

家住魔仙堡 提交于 2019-12-11 06:17:22
问题 Hi i am working on a RoR project with ruby-2.5.0 and rails 5. I am using the jsonapi-serializers for my api. I have a serializer with custom attribute as follows:- class ReceiptPartialSerializer include JSONAPI::Serializer TYPE = 'receipt' attribute :id attribute :receipt_partials attribute :receipt_partials do receipt_container = [] object.receipt_partials.each do |partial| receipt_partial = {} receipt_partial['id'] = partial.id receipt_partial['image_url'] = 'https:' + partial&.picture&.url

Rspec: How to test an exception is raised in private method?

孤街醉人 提交于 2019-12-11 05:17:43
问题 Getting error while testing an exception in a private method. How to test an exception is raised in private method which is called from a public method? Public def public_method private_method end Private def private_method tries = 0 begin raise Product::StaleObjectError.new("Product is changed while you were editing") if stale_object? // Do some work raise Exception.new("Total amount used is greater than approved") if total_approved < 0 // Save Product rescue Product::StaleObjectError => e

Testing .where queries in rspec-rails

南楼画角 提交于 2019-12-11 04:36:01
问题 I'm quite new to Rspec testing in Rails and I'm trying to test the following: class Event < ApplicationRecord def host_details query ||= User.where(id: self.user_id) end def hosting_company company = host_details.first.company end end I have the following spec but I'm not sure exactly what I'm supposed to be expecting back for the results of the host_details method. I'm using FactoryGirl. describe "gets information about the host of an event" do before(:each) do @event = build(:event) end

Different configurations for features test and rspec

陌路散爱 提交于 2019-12-11 04:12:35
问题 I'm facing a strange issue after updated rails from 4.2 to 5.2. My features test needs following configuration to run config.redis_config = { driver: :hiredis, host: ENV['REDIS_HOST'] || 'redis', port: ENV['REDIS_PORT'] || 6379 } with above config I get error unknow service for model tests which need following configuration config.redis_config = { host: ENV['REDIS_HOST'] || 'redis', port: ENV['REDIS_PORT'] || 6379 } or config.redis_config = { host: ENV['REDIS_HOST'] || 'localhost', port: ENV[

Using Factory Girl with Rspec Views

牧云@^-^@ 提交于 2019-12-11 04:05:06
问题 So I want to test some views using Rspec and Factory Girl, but I don't know how to assign a factory to a view properly. All of the information I see online uses stubbing, and although stubbing is fine, I want to keep my rspec stuff somewhat consistent. I want to use a factory for an edition model and associate that with the page when I render the page, but everything I've considered seems to be broken. Here's sort of a ridiculous example: require 'spec_helper' describe "catalog/editions

Using rails' “post” in controller tests in rspec with scoping and protocol on routes

牧云@^-^@ 提交于 2019-12-10 21:42:35
问题 I have a rails project that is running out of a subdirectory of the base url in production and I want it to act that way in dev so as to make dev and prod as close as possible. I have the routes file set up like so: Foo::Application.routes.draw do def draw_routes root :to=>'foo#home' resources :cats, :only=>[:create] do end if Rails.env.production? scope :protocol=>'http://' do draw_routes end else scope :path=>"/foo", :protocol=>'http://' do draw_routes end end end My CatsController is like

Why does before_action :authorize fail with 'wrong number of arguments'?

↘锁芯ラ 提交于 2019-12-10 18:29:45
问题 I have set up Pundit together with Devise for authorization on my application. In one of my controllers, I have before_action :authorize . I then have the following test: describe SomeController do before(:each) do login_user(FactoryGirl.create(:user, :user_type => :admin)) end describe "GET index" do it "it retrieves the index" do something = FactoryGirl.create(:Something) get :index assigns(:something).should eq([something]) end end end I receive the error: wrong number of arguments (0 for

Testing has_many association with RSpec

蹲街弑〆低调 提交于 2019-12-10 17:55:01
问题 I'm trying to test the Hour model with RSpec, namely the class method 'find_days_with_no_hours' that behaves like a scope. Business has_many Hours associated through STI. find_days_with_no_hours needs to be called through a Business object and I can't figure out how to set this up in the RSpec test. I want to be able to test something like: bh = @business.hours.find_days_with_no_hours bh.length.should == 2 I've tried various approaches, like creating a Business object (with, say, Business