FAQ Database Discussion Community


How to stub self methods in rails controller

ruby-on-rails-4,rspec3
I have a controller with the following code. def apply file_id = params[:file_id] if file_id.nil? && params[:file].nil? render json: '"No resume was provided"', status: :unprocessable_entity return end if params[:job_id].nil? render json: '"Invalid data was posted"', status: :unprocessable_entity return end job = Job.find(params[:job_id]) if Job.exists?(params[:job_id]) if job.nil? render json: '"Unable to...

Rails rspec render_template error

rspec3,ruby-on-rails-4.2
I'm following Hartl's Ruby on Rails tutorial and can't figure out why the following code keep on giving me this error: Failure/Error: expect(response).to render_template("membres/show") NoMethodError: undefined method `body' for nil:NilClass The code: require 'rails_helper' RSpec.describe "Membres", :type => :request do include Capybara::DSL describe "une inscription" do describe "ratée" do it...

Factory Girl & Rspec controller test failure

ruby-on-rails,rspec,tdd,factory-girl,rspec3
I am still fairly new to testing and still wrapping my head around Factory Girl, which I believe to be the culprit of this failure. As simple as the solution will probably be, I have searched other posts with the same failure message but the answers are not working for...

Can I alias a nested RSpec matcher?

ruby,rspec,rspec3,rspec-expectations
I have several RSpec examples that share the following complex expectation, with the array records and the floating point numbers min_long, max_long, min_lat, max_lat varying between these examples. expect(records).to all have_attributes( shape: have_attributes( exterior_ring: have_attributes( points: all( have_attributes( longitude: be_between(min_long, max_long), latitude: be_between(min_lat, max_lat) ) ) ) ) ) (The...

Validate presence of attributes using RSpec

rspec-rails,rspec3
Some attributes in my model have presence validation and I wanted to add tests in my spec to check if an error is generated when the attribute is blank. I'm using this code: it 'should have a name' do expect(@patient.errors[:name].size).to eq(1) end But here is the result of the rspec...

Ruby: How do I include methods from a Module into a subclass of ActiveModel::Serializer

ruby-on-rails,active-model-serializers,rspec3
I'm trying to include a method from a helper module into an ActiveModel::Serializer subclass but for some reason the method is not showing up. Here's my simple helper module: module Helpers module Serialisers def enforce_zulu_time(attribute) define_method(attribute) do object.send(attribute).utc.iso8601 unless object.try(attribute).nil? end end end end And here's my test serialiser class...

uninitialized constant User (NameError)

ruby-on-rails,ruby,gem,factory-girl,rspec3
I have a gem where I have moved the core parts of all the models from my main app into modules that are essentially mixins or "concerns". Thus allowing me to use those mixins from the gem in models of various applications and thus allowing me to replicate the tables...

Stubbed method can't be found in rspec test

ruby-on-rails-4,rspec3
In my controller I have code like... def apply ...do some validation stuff jobapp = JobApplication.new jobapp.apply_for_job(params, job) end In my test I want to make sure that after all the validation passes, that the apply_for_job method was called so I have the following test. describe 'apply' do before(:each) do...

`require': cannot load such file — spec_helper (LoadError)

ruby,rspec,rspec2,rspec-rails,rspec3
I am creating bundler gem --test=rspec MyGem. in which i m getting reository structure. while i m runing rspec code i m getting error. `require': cannot load such file -- spec_helper (LoadError) I try to apply require relative still i m getting error like [email protected]:~/sheetal/spec$ rspec sheetal_spec.rb \/home/sheetal/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot...

RSpec 3: expect object.do_something to NOT raise a particular kind of error

ruby,rspec3
I would like to test whether a particular method does NOT raise an error of class AError. It can raise BError, ArgumentError, almost any other kind of error, or no error, just not AError. Is there any non-deprecated (as of RSpec 3) way to do this? I tried expect {...

Error “undefined method `<' for nil:NilClass” when testing a model with RSpec.

ruby-on-rails,ruby-on-rails-4,rspec,rspec3
I am learning RSpec and testing the below scenario: require 'rails_helper' RSpec.describe Student, type: :model do before(:each) do Student.delete_all end it "roll number can't be less than 0 or more than 80" do s = Student.create(:name => "sanjay" , :lastname => "test" , :roll => 0) s1 = Student.create(:name =>...

Test file and testing file talking through each other

ruby,rspec,rspec3
I'm learning Ruby and RSpec, and I've hit a snag wherein most learning materials available have become deprecated and I lack the vocabulary to sift through the wreckage. class Session def initialize(winning_score = 0) @winning_score = winning_score play end def play get_players max_score while #game is in play print_score #play...

RSpec 3 - Test controller action that does not have routes

ruby-on-rails,ruby-on-rails-4,rspec,controller,rspec3
I have a controller and there is an action within that controller. That action contains only a few lines of code and this method doesn't have any routes, as follows: class MyController < ApplicationController def action_a # an action does have routes ..some code.. end def action b #(an action...

Getting Started with RSpec v3 in Rails 4

ruby-on-rails,ruby-on-rails-4,rspec,rspec3
Not your typical question to show on here but every tutorial seem to do this different. For most it works for them but for me, not. I have a user model in my rails app and I have installed rspec. In my rails app dir, I ran: rspec --init created...

Get the actual value of a boolean attribute

ruby,page-object-gem,rspec3,rspec-expectations
I have the span: <span disabled="disabled">Edit Member</span> When I try to get the value of the disabled attribute: page.in_iframe(:id => 'MembersAreaFrame') do |frame| expect(page.span_element(:xpath => "//span[text()='Edit Member']", :frame => frame).attribute('disabled')).to eq("disabled") end I get: expected: "disabled" got: "true" How do I get the value of specified attribute instead of a...

Rspec Does Not Recognize Rails's content_tag

ruby-on-rails,ruby-on-rails-4,rspec,rspec3
I have a Rails (4.2) helper that I am trying to unit test with Rspec 3. # app/helpers/nav_helper.rb module NavHelper def nav_link(body, url, li_class: "", html: {}) li_class += current_page?(url) ? " active" : "" content_tag(:li, class: li_class) do link_to(body, url, html) end end end # spec/helpers/nav_helper_spec.rb require 'spec_helper' describe...

Disable css in test environment for Rspec

capybara-webkit,rspec3,ruby-on-rails-4.2
I'm using rails 4 and rspec with capybara for testing. I want to disable css in my test environment. How can I do that? Can anyone help me.

RSpec matcher that checks collection to include item that satisfies lambda

ruby,rspec,rspec-rails,rspec3
I am a bit at a loss as to how to write a RSpec 3.2.x spec that checks wether a list contains at least one item that satisfies a condition. Here is an example: model = Invoice.new model.name = 'test' changes = model.changes expect(changes).to include { |x| x.key == 'name'...

How do I test the comments of the current_user in RSpec?

ruby-on-rails,ruby-on-rails-4,rspec,rspec-rails,rspec3
So I have a User that has_many :comments. In my Comments#Index, I have this: def index @comments = current_user.comments end Inside my Rspec.config... block in rails_helper.rb I have this: # Add Config info for Devise config.include Devise::TestHelpers, type: :controller My comments_controller_spec.rb looks like this: describe 'GET #index' do it "populates...

Rails, rspec: using “send()” to dynamically generate capybara URLs for controller specs

ruby,ruby-on-rails-4,metaprogramming,rspec3
Inside a controller spec for a #show action: resource_name = "adoption_transfer_type" it "returns a single resource" do resource = create(resource_name.to_sym) # Works xhr :get, api_v1_lookups_adoption_transfer_type_path(resource.id) # Does not work xhr :get, send("api_v1_lookups_#{resource_name}_path(#{resource.id})") end It results in the following error: Failure/Error: xhr :get, send("api_v1_lookups_adoption_transfer_type_path(#{resource.id})") NoMethodError: undefined method `api_v1_lookups_adoption_transfer_type_path(66)' It looks like...