rspec-rails

RSpec stub helper method in controller spec

江枫思渺然 提交于 2019-12-31 14:16: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. 回答1: You can stub it from the controller spec: controller.stub!(:signed_in?).and_return(true) # emulate signed in user controller.stub!(:signed_in?)

Oauth Model Concern Test Coverage Stubs

允我心安 提交于 2019-12-31 05:40:34
问题 I am trying to figure out the best way to reach 100% test coverage on this class. I have outlined my full spec and I am hoping someone can point me in the right direction. My assumption is stubbing the Oauth2 request would do this, but I can not seem to make that work. I'm using Rails 4. Spec 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

rspec before(:each) hook - conditionally apply

╄→гoц情女王★ 提交于 2019-12-25 04:29:15
问题 I have following in my rails_helper.rb : RSpec.configure do |config| # ... config.before(:each, type: :controller) do # SOMETHING end end I want to define directories, to which this SOMETHING will be applicable (in my case ONLY to files under spec/controllers/api directory). Any chance to achieve that? 回答1: You can use a more specialized name for your RSpec filter: RSpec.configure do |config| # ... config.before(:each, :subtype => :controllers_api) do # SOMETHING end end And then in your

How can I avoid using 'FactoryGirl.reload' due to using 'sequence' in my factories?

微笑、不失礼 提交于 2019-12-25 03:21:50
问题 Having to use FactoryGirl.reload likely to put some overhead on the time it takes to run all tests (when many tests exist) and also it is described as an Anti-Pattern. How can I keep using the sequencing for unique fields, yet test for the correct values? You can see in my code that... I have to call FactoryGirl.reload so that the increment starts at 1 in case any previous tests have already been run. I have to state :count => 1 because there will only ever be one instance of that value. spec

Rails RSpec request spec fails because unexpected %2F added to redirect response

寵の児 提交于 2019-12-24 17:08:09
问题 In config/routes.rb: get 'books(/*anything)' => redirect( "/public/%{anything}" ), :format => false In spec/requests/store_request_spec.rb: get '/books/aoeu/snth' expect(response).to redirect_to( '/public/aoeu/snth' ) This fails with: Failure/Error: expect(response).to redirect_to( '/public/aoeu/snth' ) Expected response to be a redirect to <http://www.example.com/public/aoeu/snth> but was a redirect to <http://www.example.com/public/aoeu%2Fsnth>. Expected "http://www.example.com/public/aoeu

Specifying minimum length when using FFaker::Internet.user_name

ぐ巨炮叔叔 提交于 2019-12-24 17:07:35
问题 I have a spec that keeps failing because FFaker::Internet.user_name generates a word that is less than 5 characters. How do I specify a minimum length in this stmt: username { FFaker::Internet.user_name } 回答1: From what I see you try to use FFaker in your factory. Why overcomplicate things for your specs, when you could define sequence sequence(:username) do |n| "username-#{n}" end But the question is valid and you may have some legitimate needs to use ffaker, and there are many ways to do it

ActionController::UrlGenerationError: missing required keys: [:id]

房东的猫 提交于 2019-12-24 10:35:36
问题 I'm running RSpec 3 tests and am getting this same error for this one particular path: Failure/Error: visit book_path ActionController::UrlGenerationError: No route matches {:action=>"show", :controller=>"books"} missing required keys: [:id] My test isn't quite finished, so I'm sure some of the latter code might be incorrect. But I can't get my visit path line to run: ... book = FactoryGirl.build(:book) reviews = FactoryGirl.build_list(:review, 5, book: book) visit book_path reviews.each do

How to test a nested form with RSpec and FactoryGirl?

萝らか妹 提交于 2019-12-24 05:38:09
问题 In my Rails project I have a User that can have many Projects which in turn can have many Invoices . Each Invoice can have many nested Items . class Invoice < ActiveRecord::Base attr_accessible :number, :date, :recipient, :project_id, :items_attributes belongs_to :project belongs_to :user has_many :items, :dependent => :destroy accepts_nested_attributes_for :items, :reject_if => :all_blank, :allow_destroy => true validates :project_id, :presence => true def build_item(user) items.build(:price

How can I include javascript with javascript_include_tag at the top of the page for a capybara test

一个人想着一个人 提交于 2019-12-23 23:16:26
问题 I am trying to test one javascript class in a spec test with capybara. I have static page player.haml in which I am loading the javascript class definition with javascript_include_tag. It looks like that .container = javascript_include_tag "../player" #player My test looks like that it 'can autoplay a video' do visit player_path video = Fabricate(:video) options = { autoplay: true, ... } page.execute_script("var options = '#{options}'; videoId = '#{video.id}'; var player = new Player(videoId,

Capybara & RSpec

こ雲淡風輕ζ 提交于 2019-12-23 13:03:23
问题 I can't make Capybara to work successfully, it complains that has_text is an undefined method. I have created a new rails 3.1 project ( rails new test -T ). Gemfile: source 'http://rubygems.org' gem 'rails', '3.1.3' gem 'sqlite3' group :assets do gem 'sass-rails', '~> 3.1.5' gem 'coffee-rails', '~> 3.1.1' gem 'uglifier', '>= 1.0.3' end gem 'jquery-rails' group :test do gem 'rspec-rails' gem 'capybara' end I have installed the spec folder: rails g rspec:install . spec/spec_helper.rb: ENV[