RSpec send_file testing
From Googling around, it appears that render will also be called at some point .. but with no template, will cause an error. The solution seems to be to stub it out as well: controller.stub!(:render)
From Googling around, it appears that render will also be called at some point .. but with no template, will cause an error. The solution seems to be to stub it out as well: controller.stub!(:render)
It is also possible using the having_attributes alias: expect(my_array).to include( an_object_having_attributes(id: 5) ) or, as in my own use case, matching the whole array: expect(my_array).to contain_exactly( an_object_having_attributes(id: 5), an_object_having_attributes(id: 6), an_object_having_attributes(id: 2) )
Routing specs/tests specialize in testing whether a route maps to a specific controller and action (and maybe some parameters too). I dug into the internals of Rails and Journey a bit. RSpec and Rails (basically, some details left out) use Rails.application.routes.recognize_path to answer the question “is this routable?” For example: $ rails console > Rails.application.routes.recognize_path(“/business_users/1”, … Read more
In my code, a Rails 3 project, using RSpec 2, that is exactly the line I write: describe “GET ‘index'” do before do get ‘index’ end it “should be successful” do response.should be_redirect end it “should show appropriate flash” do flash[:warning].should == I18n.t(‘authorisation.not_authorized’) end end So I am not sure why you say it is … Read more
You could think of a mock (or double) as a fake object. When you’re testing and need to work with an object that isn’t easily usable inside your test, you could use a mock as an approximation for how you expect that object to behave and work around it. Stubs can be used in a … Read more
Manually update your LOAD PATH in spec_helper.rb before calling require should do the trick. Try making this the first line of your spec_helper.rb: $: << ‘../lib’ or $LOAD_PATH << ‘../lib’ ($: is an alias for $LOAD_PATH)
I would rather use FactoryBot’s after(:create) callback to create roles (also see this issue for Rolify). Furthermore the method has_role? is used to check if a user has a specific role, to set a specific role you should use the add_role method. FactoryBot.define do factory :user do name ‘Test User’ email ‘[email protected]’ password ‘please’ password_confirmation … Read more
After running rails generate rspec:install Place your *_spec.rb files under (in your example) c:\rails_projects\sample_app\spec\model. Then specify relative path with require_relative require_relative ‘../spec_helper’
You can declare the definitions of factories as follow: FactoryGirl.define do factory :task, class: ‘Task’ do shop currency value 10 end factory :counter_task, parent: :task, class: ‘CounterTask’ do end end And now you can test base class isolated from its own factory and the same for each inherited class.
Ruby mocking frameworks have evolved a lot since this question has been asked in 2009. So here is a little 2013 comparison: Expectations with Rspec-mocks: expect(user).to receive(:say_hello) with Mocha: user.expects(:say_hello).once Stubbing an object with Rspec-mocks: user = double(name: ‘John Doe’) with Mocha: user = stub(name: ‘John Doe’) Stubbing anything with Rspec-mocks: User.any_instance.stub(:name).and_return(‘John Doe’) with Mocha: … Read more