Тест Rails 3.1 Rspec не работает в POST-контроллере POST 'create'

У меня есть тест, который не работает, и я просто не могу понять, как заставить его работать. Это тест «должен иметь правильный заголовок» в разделе «POST Create». Это приложение Rails 3.1, вот мой файл gem для тестирования:

group :test do
  gem 'turn', :require => false
  gem 'minitest'
  gem 'spork', '> 0.9.0.rc'
  gem 'guard-spork'
  gem 'guard-rspec'
  gem 'webrat'
  gem 'factory_girl_rails', '> 1.0'
  gem 'mocha'
  gem 'rspec-rails'
end

Контроллер моих сообщений

class PostsController < ApplicationController

  def new
    @post = Post.new
    @title = "Create a new post"

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @post }
    end
  end

  def show
    @post = Post.find(params[:id])
    @title = @post.title

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @post }
    end
  end

  def create
    @post = Post.new(params[:post])

    respond_to do |format|
      if @post.save
        format.html { redirect_to @post, notice: 'Post was successfully created.' }
        format.json { render json: @post, status: :created, location: @post }
      else
        format.html { render action: "new" }
        format.json { render json: @post.errors, status: :unprocessable_entity }
        @title = "Create a new post"
      end
    end
  end

end

Тесты Rspec — я добавил в файл render_views.

describe "POST 'create'" do

    describe "SUCCESS" do

      before(:each) do
        @attr = { :title => "Foo", :content => "Bar" }
      end

      it "should create a new post" do
        lambda do
          post :create, :post => @attr
          flash[:notice].should_not be_nil
        end.should change(Post, :count).by(1)
      end

      it "should redirect to the post show page and show success message" do
        post :create, :post => @attr
        flash[:notice].should =~ /post was successfully created/i
        response.should redirect_to post_path(assigns(:post))
      end

      it "should have the right title" do
        post :create, :post => @attr
        response.should have_selector("title", :content => assigns(:post).title)
      end
    end
end

Не проходит только последний тест. Сообщения об ошибках:

Finished in 6.49 seconds
11 examples, 1 failure

Failed examples:

rspec ./spec/controllers/posts_controller_spec.rb:62 # PostsController POST 'create' SUCCESS should have the right title
Running: spec/controllers/posts_controller_spec.rb
.......F...

Failures:

  1) PostsController POST 'create' SUCCESS should have the right title
     Failure/Error: response.should have_selector("title", :content => assigns(:post).title)
       expected following output to contain a <title>Foo</title> tag:
       <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
       <html><body>You are being <a href="http://test.host/posts/980190963">redirected</a>.</body></html>
     # ./spec/controllers/posts_controller_spec.rb:64:in `block (4 levels) in <top (required)>'

Любая помощь относительно того, почему это не удается, будет принята с благодарностью.


person veritas1    schedule 14.12.2011    source источник


Ответы (1)


Вы желаете чего-то, что просто не произойдет.

Поскольку вы перенаправляете, ответ будет таким:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body>You are being <a href="http://test.host/posts/980190963">redirected</a>.</body></html>

Лучше протестировать сам новый объект.

person apneadiving    schedule 14.12.2011
comment
Спасибо. Я только что сам это понял :) - person veritas1; 15.12.2011