rspec-rails

How to mock and stub active record before_create callback with factory_girl

只谈情不闲聊 提交于 2019-12-02 20:42:40
I have an ActiveRecord Model, PricePackage. That has a before_create call back. This call back uses a 3rd party API to make a remote connection. I am using factory girl and would like to stub out this api so that when new factories are built during testing the remote calls are not made. I am using Rspec for mocks and stubs. The problem i'm having is that the Rspec methods are not available within my factories.rb model: class PricePackage < ActiveRecord::Base has_many :users before_create :register_with_3rdparty attr_accessible :price, :price_in_dollars, :price_in_cents, :title def register

Factory-girl create that bypasses my model validation

怎甘沉沦 提交于 2019-12-02 16:28:26
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 validation in the model that checks a new group's expiry is after today's date. This raises the validation

Rails 3.1, RSpec: testing model validations

时光怂恿深爱的人放手 提交于 2019-12-02 16:20:31
I have started my journey with TDD in Rails and have run into a small issue regarding tests for model validations that I can't seem to find a solution to. Let's say I have a User model, class User < ActiveRecord::Base validates :username, :presence => true end and a simple test it "should require a username" do User.new(:username => "").should_not be_valid end This correctly tests the presence validation, but what if I want to be more specific? For example, testing full_messages on the errors object.. it "should require a username" do user = User.create(:username => "") user.errors[:username]

How do I turn on SQL debug logging for ActiveRecord in RSpec tests?

被刻印的时光 ゝ 提交于 2019-12-02 14:25:21
I have some RSpec tests for my models and I would like to turn on SQL ActiveRecord logging just like I see in the Rails server mode. How to do that? I start my tests with RAILS_ENV=test bundle exec rspec my/test_spec.rb Thanks idlefingers By default, all your db queries will be logged already in test mode. They'll be in log/test.log . You could try setting the ActiveRecord logger to stdout in your test somewhere. If you're using rspec, maybe in the spec helper? ActiveRecord::Base.logger = Logger.new(STDOUT) set config.log_level = :info in test environment if others answers don't work in your

Oauth Model Concern Test Coverage Stubs

天涯浪子 提交于 2019-12-02 11:32:46
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_authentication_spec') unfiltered_oauth_packet['provider'] = unfiltered_oauth_packet['provider'].to_sym

Why Capypara + Rspect tests still pass even though I delete application.js file?

馋奶兔 提交于 2019-12-02 05:39:17
问题 I have rails app and have some ajax call in application.js file. O have rspec, capybara, webkit, factorygirl and databasecleaner for testing. I wrote feature tests which all pass. Then accidentally delete application.js file and run the tests, they still pass. Please help me to find out why. Thanks so much. PS: if you downvote my question, please at least tell me why. I have been searching for answers but couldn't find one. 回答1: Try with rake assets:clobber This will remove compiled assets

RSpec matcher that checks collection to include item that satisfies lambda

送分小仙女□ 提交于 2019-12-02 04:06:18
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' && x.value == 'test' } There will be other (automated) changes in the changes list too so I don't want to verify that there is only one specific change and I also don't want to rely on ordering expect(changes.first)... so I basically need a way to specify that at least one change in the list satisfies the condition. I could

Rails 4 and RSpec, undefined method `assertions' in routing spec

不问归期 提交于 2019-12-01 19:46:26
问题 I'm having what appears to be the same issue as undefined method `assertions' in routing spec but I'm running Rails 4 and it is locked to minitest 5.0 or greater. Thus the solution to use minitest 4.7 I can't make work. Is there a work around for this? Where would I report the bug? 回答1: This problem is caused by a change in minitest 5.0 documented here: # https://github.com/seattlerb/minitest/issues/286 is fixed by using: gem "rspec-rails", '~> 2.14.0.rc1' in the gemfile. 来源: https:/

How to find a variable with rspec tests using page.find

依然范特西╮ 提交于 2019-12-01 14:18:59
I have a test case, most of my other pages have at least one field that is just straight text and can be found using: page.find("tr", text: "What I filled in").find("a.tick").click This page all of the options are drop down selects, so how do I find a variable? The rest of the syntax looks like this: it "edits person job and redirects to index" do expect(p = FactoryGirl.create(:person)).to be_valid() expect(j = FactoryGirl.create(:job)).to be_valid() visit new_job_path select p.name, from: "person_job_person_id" select p.name, from: "person_job_job_id" click_button "create person job" page

RSpec controller specs fail after adding authorization to Rails4 application

余生颓废 提交于 2019-12-01 12:39:46
Following Railscast #385 & #386 I have added authorization to my Rails 4 application (rails 4.2.6, rspec-rails 3.4.2). After adding authorization, all of my controller specs fail. My feature specs still pass because in the spec I login as admin to perform actions that are not allowed visitors (:edit, :update...). If I make all actions allowed to a visitor my controller specs pass. However, any action that only the admin can perform will fail in the controller spec. The scaffold generator creates an empty let(:valid_session) { { } } that it says in the comments: # This should return the minimal