Testing with rspec - problems with stub and should

Asked

Viewed 52 times

2

Hello. I am new to testing and have two tests with problems, one of helper and one of model, being:

1) TodosHelper TodosHelper#visibility(todo) when todo is public 
 Failure/Error: before { helper.stub :current_user => current_user }

 NoMethodError:
   undefined method `stub' for #<#<Class:0x00000006d3c9e8>:0x00000006d723b8>
 # ./spec/helpers/todos_helper_spec.rb:10:in `block (3 levels) in <top (required)>'

3)  Bookmark Bookmark user watch a todo when todo is public can creates a bookmark relationship with todo
   Failure/Error:
     lambda {
      FactoryGirl.create(:bookmark, :todo => todo)
     }.should change(user.bookmarks, :count).by(1)

 NoMethodError:
   undefined method `should' for #<Proc:0x00000006f783b0>
 # ./spec/models/bookmark_spec.rb:21:in `block (5 levels) in <top (required)>'

I am using the following Gems:

group :development, :test do
  gem 'byebug'
  gem "rspec-rails"
  gem 'capybara'
  gem "factory_girl_rails", "~> 4.0"
  gem 'rspec-activemodel-mocks'
end

and testing, model:

describe "watch a todo" do

      context "when todo is public" do

        let(:todo) { FactoryGirl.create(:todo, :public => true) }

        it "can creates a bookmark relationship with todo" do

          lambda {
            FactoryGirl.create(:bookmark, :todo => todo)
          }.should change(user.bookmarks, :count).by(1)

          user.bookmark?(todo).should be_true
        end
      end
    end

helper:

RSpec.describe TodosHelper, type: :helper do

  describe TodosHelper do

    let(:current_user) { FactoryGirl.create(:user) }
    let(:todo) { FactoryGirl.create(:todo) }
    before { helper.stub :current_user => current_user }
    describe "#visibility(todo)" do
      subject { helper.visibility(todo) }
      context "when todo is public" do
        before { todo.stub(:public? => true) }
        it { should eq "public" }
          end
...

Does anyone have any idea? that methods are deprecated?

1 answer

0

Try:

helper = double
helper.stub(current_user: current_user)

Or:

helper = double
helper.stub(:current_user).and_return(current_user)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.