Lester Peabody
Lester Peabody

Reputation: 1888

Need help writing this RSpec test

I'm new to RSpec and BDD. I'm also a Railscast junkie, and I've been watching as much as I can with regards to testing, RSpec, BDD, and Capybara. Specifically I've been watching Episode 275 over and over, which covers using Guard to auto-run your tests once you save your spec, and touches on Capybara and integration specs.

So that's my background when it comes to BDD and RSpec/Capybara right there. I literally started today. Now my situation is this:

I have a model Task. When I create a new task, I should get a flash message up top saying it was created successfully and be directed to a new task page. However, before I implement this, I want to write a test first to demonstrate this functionality (as Uncle Bob once said, you should never write any code until you've written at least one failing test). I'm a bit lost as to what types of tests I should write though. Would this be an integration spec or a controller spec? Or both? What would these tests look like?

Upvotes: 0

Views: 65

Answers (1)

zetetic
zetetic

Reputation: 47548

Here's a generalized example to help you get started:

describe "Creating a new Task" do
  before do
    # setup tasks for logging in a user with sufficient rights
    # create any objects ont which the new Task depends

    visit "/tasks/new"
    fill_in "name", :with => "Sample Task"
    click_button "Submit"
  end

  it "should show a success message" do
    page.should have_content "Task was created successfully"
  end

  it "should redirect to the show task page" do
    page.should have_content "Show Task"
    task = Task.last
    current_path.should == task_path(task)
  end
end

Upvotes: 1

Related Questions