How can I stub find_each for rspec testing in rails 3
You can use and_yield to make rspec call the block passed to the mock: MyClass.stub(:find_each).and_yield(one_mock).and_yield(two_mock)
You can use and_yield to make rspec call the block passed to the mock: MyClass.stub(:find_each).and_yield(one_mock).and_yield(two_mock)
Did you recently upgrade to RSpec 3 like I did? This is from the RSpec 3 documentation: Automatically Adding Metadata RSpec versions before 3.0.0 automatically added metadata to specs based on their location on the filesystem. This was both confusing to new users and not desirable for some veteran users. In RSpec 3, this behavior … Read more
I think the best way is to use rspec build in output matcher https://www.relishapp.com/rspec/rspec-expectations/docs/built-in-matchers/output-matcher Fore example, this is your class class MakeIt def awesome(text) puts “Awesome #{text}” end end and your test describe MakeIt do describe ‘#awesome’ do it ‘prints awesome things’ do expect do MakeIt.new.awesome(‘tests’) end.to output(‘Awesome tests’).to_stdout end it ‘does not print not … Read more
I understood the difference between let and let! with a very simple example. Let me read the doc sentence first, then show the output hands on. About let doc says :- … let is lazy-evaluated: it is not evaluated until the first time the method it defines is invoked. I understood the difference with the … Read more
Assumptions This answer assumes you have the rspec-rails, active_model_serializers and factory_girl_rails gems installed and configured. This answer also assumes you have defined a factory for the Sample resource. Serializer spec For the current version(0.10.0.rc3) of active_model_serializers at the time of writing, ActiveModel::Serializer classes do not receive to_json and are , instead, wrapped in an adapter … Read more
Try current_path.should == “/projects/show” Capybara also implements a current_url method for the fully qualified URL. More info in the docs. EDIT Capybara now has a RSpec matcher called have_current_path, which you can use like: expect(page).to have_current_path(some_other_page_path) (thanks @bjliu)
(I might be biased because I am involved with SpecFlow, but here my thoughts…) Cuke4Nuke is very close to Cucumber. This promises a lot of advantages: Compatibility Getting new features from Cucumber when Cucumber evolves (at least in theory, but language support is an example for this) Being a real part of the Cucumber community … Read more
In order to get rid of the warning with your code as-is, you’ll have to explicitly enable the should syntax in your config: RSpec.configure do |config| config.expect_with :rspec do |c| c.syntax = [:should, :expect] end end The replacement syntax for stub_chain is: allow(object).to receive_message_chain(:one, :two, :three).and_return(:four) expect(object.one.two.three).to eq(:four) More information about this and its usage … Read more
You do not need the DatabaseCleaner gem. Here’s a quick summary of steps you need: Remove capybara-selenium gem and database_cleaner gem Add selenium-webdriver gem Make sure you are using Rails 5.1.5 or later. Earlier versions of Rails 5.1 had a defect in ActionDispatch::SystemTesting::Server that caused problems (fixed here). In your rails_helper file, set config.use_transactional_fixtures = … Read more
This is explained in the Guide to Testing Rails Applications in section 4 “Functional Tests for your Controllers. The get action takes a params hash and a session hash, e.g.: get(:show, {‘id’ => “12”}, {‘user_id’ => 5}) You should be able to pass in nil for the params hash in your example, then pass in … Read more