How do I generate a controller spec using rspec?

the rspec way is rails g rspec:controller passwords it gives create spec/controllers/passwords_controller_spec.rb –Update You can configure your application to generate rspec when model or controller is created. Add to config/application.rb # don’t generate RSpec tests for views and helpers. config.generators do |g| g.test_framework :rspec, fixture: true g.fixture_replacement :factory_girl, dir: ‘spec/factories’ g.view_specs false g.helper_specs false end … Read more

Testing ActionMailer multipart emails(text and html version) with RSpec

To supplement, nilmethod’s excellent answer, you can clean up your specs by testing both text and html versions using a shared example group: spec_helper.rb def get_message_part (mail, content_type) mail.body.parts.find { |p| p.content_type.match content_type }.body.raw_source end shared_examples_for “multipart email” do it “generates a multipart message (plain text and html)” do mail.body.parts.length.should eq(2) mail.body.parts.collect(&:content_type).should == [“text/plain; charset=UTF-8”, … Read more

In RSpec, using let variable inside before :all block

I had the same problem, I have solved it by declaring all my variables as attributes inside the before block: describe ‘index’ before(:all) do @company = FactoryGirl.create(:company) @user = FactoryGirl.create(:user, company: @company) sign_in @user visit products_path end … end Now you can use @user and @company inside your tests, and you shouldn’t have any warnings.

Allow vs Stub, what’s the difference?

There are 2 differences but the result is exactly the same. Both are in regards to the rspec mocks/expectations syntax. Use of #allow instead of #stub method. First case uses the new rspec syntax introduced this year. This is now the preferred way of using rspec. Altough the old syntax isn’t deprecated, it will probably … Read more

What is the difference between mock and mock_model in RSpec

As jenger said mock_model is an extension built for active record: This is the source in 1.2.6: def mock_model(model_class, options_and_stubs = {}) id = options_and_stubs[:id] || next_id options_and_stubs = options_and_stubs.reverse_merge({ :id => id, :to_param => id.to_s, :new_record? => false, :errors => stub(“errors”, :count => 0) }) m = mock(“#{model_class.name}_#{id}”, options_and_stubs) m.__send__(:__mock_proxy).instance_eval <<-CODE def @target.as_new_record self.stub!(:id).and_return … Read more

Rspec let scoping

let() is supposed to be scoped to the example blocks and unusable elsewhere. You don’t actually use let() as parameters. The reason it does not work with it_behaves_like as a parameter has to do with how let() gets defined. Each example group in Rspec defines a custom class. let() defines an instance method in that … Read more

How to test that a certain function uses a transaction in Rails and rspec 2

You should look at the problem from a different perspective. Testing whether a function uses a transaction is useless from a behavioral viewpoint. It does not give you any information on whether the function BEHAVES as expected. What you should test is the behavior, i.e. expected outcome is correct. For clarity, lets say you execute … Read more

Rspec run all tests except a specific folder

Use an –exclude-pattern, they’re quite convenient: https://www.relishapp.com/rspec/rspec-core/v/3-3/docs/configuration/exclude-pattern One of the nice things about them: The –exclude-pattern flag accepts shell style glob unions So you could do something like rspec –exclude-pattern “spec/{foldername1,foldername2}/**/*_spec.rb”

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)