rspec-rails

RSpec-rails-capybara - different failures with :js => true and without

。_饼干妹妹 提交于 2019-12-03 12:42:46
问题 I'm building a setup screen for billing of individuals. The controller/views are in the Admin namespace. When run the first test without :js => true I get one failure, which I assume is down to the fact that the link does not work as its a helper which uses a js script to build a nested set of fields (Based on Railscasts Single form, multiple tables - Nested Attributes). Failures: 1) Patient Setup create patient bill heading - with extended details -with valid data Failure/Error: fill_in

Mocking file uploads in Rails 3.1 controller tests

放肆的年华 提交于 2019-12-03 12:40:14
My controller accesses the tempfile attribute of an uploaded file and passes it to another mocked component. My test code has @file = mock(Object) @file.stub_chain(:tempfile, :path).and_return('thefile.zip') # ... post :create, :file => @file and the controller code calls params[:file].tempfile.path . After upgrading from Rails 3.0 to 3.1, the above line started failing with undefined method `tempfile' for "#[RSpec::Mocks::Mock:0x2b0d9a0 @name=Object]":String That is, Rails 3.1 converted params[:file] to a string automatically. The code works properly when tested manually through a browser. I

Sending custom headers through RSpec

蓝咒 提交于 2019-12-03 08:35:49
问题 Given my API consumers are required to send a customer HTTP header like this: # curl -H 'X-SomeHeader: 123' http://127.0.0.1:3000/api/api_call.json Then I can read this header in a before_filter method like this: # 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 So far great. Now I would like to test this using RSpec as there is a change in

mock Rails.env.development? using rspec

久未见 提交于 2019-12-03 07:38:16
问题 I am writing a unit test using rspec. I would like to mock Rails.env.develepment? to return true. How could I achieve this?. I tried this Rails.env.stub(:development?, nil).and_return(true) it throws this error activesupport-4.0.0/lib/active_support/string_inquirer.rb:22:in `method_missing': undefined method `any_instance' for "test":ActiveSupport::StringInquirer (NoMethodError) Update ruby version ruby-2.0.0-p353, rails 4.0.0, rspec 2.11 describe "welcome_signup" do let(:mail) { Notifier

How can I test :inclusion validation in Rails using RSpec

旧时模样 提交于 2019-12-03 05:32:06
问题 I have the following validation in my ActiveRecord. validates :active, :inclusion => {:in => ['Y', 'N']} I am using the following to test my model validations. should_not allow_value('A').for(:active) should allow_value('Y').for(:active) should allow_value('N').for(:active) Is there a cleaner and more through way of testing this? I am currently using RSpec2 and shoulda matchers. EDIT After some looking around I only found, this probably an 'ok' way of testing this, shoulda does not provide

Rails 5, Rspec: Environment data not found in the schema

一个人想着一个人 提交于 2019-12-03 04:10:18
After upgrading a Rails app to Rails 5, running RSpec tests gives me the following error: rails aborted! ActiveRecord::NoEnvironmentInSchemaError: Environment data not found in the schema. To resolve this issue, run: bin/rails db:environment:set RAILS_ENV=test However, that bin does not exist and I can't seem to generate it with bundle binstubs rails or with rake rails:update:bin . I have also tried: rails db:environment:set RAILS_ENV=test rake db:environment:set RAILS_ENV=test There is a related issue on Github here . How can I address this error? New Rails 5 command to generate binstubs:

RSpec-rails-capybara - different failures with :js => true and without

冷暖自知 提交于 2019-12-03 03:06:59
I'm building a setup screen for billing of individuals. The controller/views are in the Admin namespace. When run the first test without :js => true I get one failure, which I assume is down to the fact that the link does not work as its a helper which uses a js script to build a nested set of fields (Based on Railscasts Single form, multiple tables - Nested Attributes). Failures: 1) Patient Setup create patient bill heading - with extended details -with valid data Failure/Error: fill_in "Extended Bill Heading", :with => 'Regular Registration' Capybara::ElementNotFound: cannot fill in, no text

Factory-girl create that bypasses my model validation

泄露秘密 提交于 2019-12-03 02:54:24
问题 I am using Factory Girl to create two instances in my model/unit test for a Group. I am testing the model to check that a call to .current returns only the 'current' groups according to the expiry attribute as per below... describe ".current" do let!(:current_group) { FactoryGirl.create(:group, :expiry => Time.now + 1.week) } let!(:expired_group) { FactoryGirl.create(:group, :expiry => Time.now - 3.days) } specify { Group.current.should == [current_group] } end My problem is that I've got

Sending custom headers through RSpec

我的梦境 提交于 2019-12-03 00:05:38
Given my API consumers are required to send a customer HTTP header like this: # curl -H 'X-SomeHeader: 123' http://127.0.0.1:3000/api/api_call.json Then I can read this header in a before_filter method like this: # 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 So far great. Now I would like to test this using RSpec as there is a change in behavior: # spec/controllers/api_controller_spec.rb describe ApiController do it "should process the

RSpec stub helper method in controller spec

萝らか妹 提交于 2019-12-02 23:56:49
Found similar questions but surprisingly none, that I've found, give a simple answer... Trying to stub a helper method in my controller spec; not quite sure what object would need to be doubled? Controller calls this method: #app/helpers/sessions_helper.rb def signed_in? current_user.present? end I'd like to stub it in spec to return true/false. You can stub it from the controller spec: controller.stub!(:signed_in?).and_return(true) # emulate signed in user controller.stub!(:signed_in?).and_return(false) # emulate unsigned user Object 'controller' is predefined in a controller specs. UPDATE: