1
votes

Is it possible to stub methods when using Capybara, particularly capybara-webkit driver?

This does not seem to work:

before do 
  User.any_instance.stub(:get_tweets)
  visit tweets_path
end

I presume it doesn't work because we're stubbing the User object in rspec land, whereas the capybara-webkit driver is executing the Rails app in a separate "thread".

Is there a best practice on mocking/stubbing in feature specs?

1

1 Answers

0
votes

Capybara typically doesn't execute your Rails app at all. It is used to hit your app's endpoints, navigate the pages and make assertions on their contents.

This means you'll need to create test data using db\seeds.rb, then run your specs against the app with the dummy data loaded.

Alternately, in some integration tests you can create the test data using your Capybara specs - for example, an integration test that creates a user and subsequently tries to update that user's profile.