rspec3

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

In RSpec, using let variable inside before :all block

半城伤御伤魂 提交于 2019-12-19 05:05:15
问题 I have the following code inside most of my tests: describe 'index' let(:company) { FactoryGirl.create(:company) } let(:user) { FactoryGirl.create(:user, company: company) } before do sign_in user visit products_path end ... end But I'm getting the following warning: WARNING: let declaration 'user' accessed in a 'before(:all)' My question is, what is the correct way of doing this? I can't find much information about the warning itself. Thanks! EDIT: My goal is to use the user variable so I

Rspec undefined method 'helpers' for ActionDispatch Routing

有些话、适合烂在心里 提交于 2019-12-14 03:54:10
问题 I'm upgrading an old rails 3 app to rails 5 and also upgrading Rspec in the process. When running test, I get an error: NoMethodError Exception: undefined method `helpers' for #<ActionDispatch::Routing::RouteSet::NamedRouteCollection:0x007ffaf09d9200> whenever I call be_success on a response. See example: it "renders 'OK' text when request is html" do get :ping expect(response).to be_success response.body.should == 'OK' end I get the error when I run this test. After inserting a breakpoint, I

Aruba not working with RSpec 3?

旧时模样 提交于 2019-12-12 15:32:52
问题 I'm trying to test a ruby command line script but am having troubles running tests with Cucumber-less Aruba in RSpec 3. Some weird errors, some obvious ones. e.g. weird: 1) test does something Failure/Error: run_simple("cli_test.rb -h", true) ArgumentError: wrong number of arguments (2 for 1) # .../.gem/ruby/2.2.0/gems/aruba-0.6.2/lib/aruba/api.rb:632:in `assert_exit_status' # .../.gem/ruby/2.2.0/gems/aruba-0.6.2/lib/aruba/api.rb:750:in `run_simple' # ./spec/cli_test_spec.rb:17:in `block (2

Rspec 3. undefined local variable or method `response' for #<RSpec::ExampleGroups::ConfigsAPI::GETApiConfig:0x007f84d93fbb90>

给你一囗甜甜゛ 提交于 2019-12-12 10:05:52
问题 I want to test API controller with rspec. So here is the simple piece of code: require 'rails_helper' describe "GET /api/config" do it 'routes GET to /api/config to Api::V1::ConfigsController#show' do get '/api/config.json' expect(response).to be_success end end But I've got an error: 1) Configs API GET /api/config routes GET to /api/config to Api::V1::ConfigsController#show Failure/Error: expect(response).to be_success NameError: undefined local variable or method `response' for #<RSpec:

How to stub i18n_scope for mocking ActiveRecord::RecordInvalid on Rails 4 rspec 3.3.0?

拜拜、爱过 提交于 2019-12-10 20:59:35
问题 I have tried this code but it raises the error: NameError: uninitialized constant RSpec::Mocks::Mock RSpec::Mocks::Mock.stub(:i18n_scope).and_return(:activerecord) model = double(:model, errors: double(:errors, full_messages: [])) ActiveRecord::RecordInvalid.new(model) How can I stub i18n_scope ? 回答1: To answer your question, you have to stub RSpec::Mocks::Double because that's the class of the instance you're actually passing to ActiveRecord::RecordInvalid. However, that won't work because

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

孤街醉人 提交于 2019-12-09 16:22:15
问题 I am creating bundler gem --test=rspec MyGem. in which I'm getting the repository structure. When I try to run the rspec code I get the following error: `require': cannot load such file -- spec_helper (LoadError) I then try to apply require relative but I still get an error: sheetal@ubuntu:~/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 load such file -- spec_helper (LoadError) from /home

RSpec: stubbing Rails.application.config value doesn't work when reopening classes?

天大地大妈咪最大 提交于 2019-12-09 09:23:30
问题 I have an option defined in application config. My class I want to test is defined in a gem (not written by me). I want to reopen the class: Myclass.class_eval do if Rails.application.config.myoption=='value1' # some code def self.method1 end else # another code def self.method2 end end end I want to test this code using RSpec 3: # myclass_spec.rb require "rails_helper" RSpec.describe "My class" do allow(Rails.application.config).to receive(:myoption).and_return('value1') context 'in taxon'

Find a newly created record in a controller test using RSpec 3 and Rails 4

丶灬走出姿态 提交于 2019-12-07 23:55:58
问题 I'm doing a controller spec in Rails 4, and I'm wanting to test the attributes of a record created by a controller action. How do I find the newly created record? For example, what could I do instead of it 'Marks a new user as pending' do post :create, params # I don't want to use the following line user = User.last expect(user).to be_pending end The Rails guides only briefly talks about controller tests, where it mentions testing that Article.count changes by 1, but not how to get a new

How can I stub a controller helper method in a view spec with RSpec 3.4?

孤者浪人 提交于 2019-12-07 17:52:13
问题 I'm testing a view with RSpec (3.4 on Rails 4.2.5). And I use the decent_exposure gem in my CRUD controller. decent_exposure makes it easy to define named methods that are made available to my views and which memoize the resultant values. But I don't understand how I can stub these methods in a view spec. I tried do it according to the RSpec documentation, but it raises an error. Why doesn't it work? How can I stub the method article in the view? My controller class Account: