Przemek Mroczek
Przemek Mroczek

Reputation: 357

Nil is not symbol in Rspec

When I run in development mode on my localhost. Everything works, I can easily remove my posts. But my request specs are failing.

I get this error, when I run my tests:

1) Posts when logged in should delete posts
     Failure/Error: expect{click_on "Usuń spot"}.to change(Post.count).by(1)
     TypeError:
       nil is not a symbol

posts_spec.rb

describe "when logged in" do
    let(:user) {FactoryGirl.create(:user)}
    let!(:post) {user.posts.create(content: "Brilliant! I just saw the most amazing ever. She looked so cute!")}
    content = "Example of spot post, for TDD. It's not real spot. Not yet."

    before(:each) {
      log_user(user)
    }

    it "should delete posts" do
      visit user_post_path(user, post)
      expect{click_on "Usuń spot"}.to change(Post.count).by(1)
    end

show.haml

[email protected]
=link_to "Usuń spot", [@user,@post], method: :delete

posts_controller.rb

  def show
    @user = current_user
    @post = @user.posts.find(params[:id])
  end

  def destroy
    @user = current_user
    @post = @user.posts.find_by_id(params[:id])
    @post.destroy

    flash[:success] = "Post destroyed"

    redirect_to root_url
  end

Upvotes: 3

Views: 2124

Answers (2)

ll14m4n
ll14m4n

Reputation: 11

change{Post, :count}.by(1)

is the correct syntax. documentation

Upvotes: 0

Dogbert
Dogbert

Reputation: 222050

change requires a block, so it can be evaluated twice to validate the change in value.

This should work:

expect{click_on "Usuń spot"}.to change{Post.count}.by(1)

Upvotes: 19

Related Questions