rspec-rails

How to mock and stub active record before_create callback with factory_girl

 ̄綄美尐妖づ 提交于 2019-12-20 10:14:38
问题 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

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

穿精又带淫゛_ 提交于 2019-12-20 08:09:12
问题 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 回答1: By default, all your db queries will be logged already in test mode. They'll be in log/test.log . 回答2: 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 =

RSpec matcher that checks collection to include item that satisfies lambda

我们两清 提交于 2019-12-20 04:40:09
问题 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

RSpec controller specs fail after adding authorization to Rails4 application

断了今生、忘了曾经 提交于 2019-12-19 10:56:13
问题 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

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

陌路散爱 提交于 2019-12-18 12:04:30
问题 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

Unable to obtain stable firefox connection in 60 seconds (127.0.0.1:7055)

我的未来我决定 提交于 2019-12-17 08:50:54
问题 While testing scenario by cucumber i'm getting the following error when running rspec tests unable to obtain stable firefox connection in 60 seconds (127.0.0.1:7055) (Selenium::WebDriver::Error::WebDriverError) Using ruby (1.9.2) selenium-webdriver (2.27.2) and firefox (19.0) Using rspec-rails (2.12.1) , capybara (2.0.2) and several other gems, Also i have added launchy gem but they don't seem to be a problem. And i am using Windows 7 . 回答1: I had the same problem (on Linux). Fixed with: gem

test a file upload using rspec - rails

流过昼夜 提交于 2019-12-17 04:40:29
问题 I want to test a file upload in rails, but am not sure how to do this. Here is the controller code: def uploadLicense #Create the license object @license = License.create(params[:license]) #Get Session ID sessid = session[:session_id] puts "\n\nSession_id:\n#{sessid}\n" #Generate a random string chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a newpass = "" 1.upto(5) { |i| newpass << chars[rand(chars.size-1)] } #Get the original file name upload=params[:upload] name = upload[

test a file upload using rspec - rails

拥有回忆 提交于 2019-12-17 04:40:15
问题 I want to test a file upload in rails, but am not sure how to do this. Here is the controller code: def uploadLicense #Create the license object @license = License.create(params[:license]) #Get Session ID sessid = session[:session_id] puts "\n\nSession_id:\n#{sessid}\n" #Generate a random string chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a newpass = "" 1.upto(5) { |i| newpass << chars[rand(chars.size-1)] } #Get the original file name upload=params[:upload] name = upload[

RSpec with devise

邮差的信 提交于 2019-12-14 03:04:34
问题 im new to rails :) im trying to run my first test. why does this test pass? username should have at least 2 characters, my username has more and it still passes test. user.rb: validates :username, :length => { :minimum => 2 } user_spec.rb require 'spec_helper' describe User do before do @user = User.new(username: "Example User", email: "user@example.com", password: "foobar", password_confirmation: "foobar") end describe "when name is not present" do before { @user.username="aaaahfghg" } it {

What does mean <top (required)> in rspec?

杀马特。学长 韩版系。学妹 提交于 2019-12-14 02:32:50
问题 I have some rspec test. For example require 'spec_helper' describe UsersController do before (:each) do @user = FactoryGirl.create(:user) sign_in @user end describe "GET 'show'" do it "should be successful" do get :show, :id => @user.id response.should be_success end it "should find the right user" do get :show, :id => @user.id assigns(:user).should == @user end end end When I run the rspec i get some errors Failures: 1) UsersController GET 'show' should be successful Failure/Error: @user =