[Unit Test for Zombie] 05. Integration Tests

RAILS INTEGRATION TEST

Using Rails integration test helpers complete the two tests below by calling thetweet_url(@tweet) path using GET, and asserting a successful response and that the h1 tag contains the @tweet.status.

class TweetsController < ApplicationController
 
  def show
    @tweet = Tweet.find(params[:id])
  end
 
end
<h1><%= @tweet.status %></h1>
<p>Posted by <%= link_to @tweet.zombie.name, zombie_tweets_path(@tweet.zombie) %></p>

Answer:

复制代码
class TweetDisplaysStatusTest < ActionDispatch::IntegrationTest
  def setup
    @tweet = tweets(:hello_world)
  end

  test "Tweet page responds successfully" do
    get tweet_url(@tweet)
    assert_response :success
  end

  test "Tweet displays status in heading" do 
    get tweet_url(@tweet)
    assert_select "h1", @tweet.status
  end
end
复制代码

 

RAILS INTEGRATION TEST - POST

Notice below how we're creating a tweet using POST. Complete the integration test below toassert that the user is redirected to the tweet page of the last Tweet created (Tweet.last).

复制代码
class TweetsController < ApplicationController
 
  def show
    @tweet = Tweet.find(params[:id])
  end
 
  def new
    @tweet = Tweet.new
  end
 
  def create
    @tweet = Tweet.new(params[:tweet])
    if @tweet.save
      redirect_to @tweet
    else
      render :new
    end
  end
 
end
复制代码

Answer:

复制代码
class CreatingATweetTest < ActionDispatch::IntegrationTest
  def setup
    @zombie = zombies(:ash)
    @tweet_attributes = {tweet: {zombie_id: @zombie.id, status: 'Test tweet'}}
  end

  test "Responds with a redirect to the tweet page" do
    post tweets_url, @tweet_attributes
    assert_redirected_to tweet_url(Tweet.last)
  end
end
复制代码

 

CAPYBARA INTEGRATION TEST

Rewrite the Rails integration test for viewing a Tweet with Capybara.

复制代码
class TweetDisplaysStatusTest < ActionDispatch::IntegrationTest
  def setup
    @tweet = tweets(:hello_world)
  end

  test "Tweet displays status in heading" do
    get tweet_url(@tweet)
    assert_select 'h1', @tweet.status
  end
end
复制代码

Answer:

复制代码
class TweetDisplaysStatusTest < ActionDispatch::IntegrationTest
  def setup
    @tweet = tweets(:hello_world)
  end

  test "Tweet displays status in heading" do
    visit tweet_url(@tweet)
    within("h1") do
      assert has_content?(@tweet.status)
    end
  end
end
复制代码

 

CAPYBARA INTEGRATION TEST II

Using Capybara, test that tweet creation works properly. The url to the tweet creation page isnew_tweet_url. Hint: You should assert that you've ended up at'tweet_path(Tweet.last)' after submitting the form. Take a close look at the new.htmlfile below to determine the labels and contents of the form fields.

复制代码
<h1>New tweet</h1>
 
<form accept-charset="UTF-8" action="/tweets" class="new_tweet" id="new_tweet" method="post">
 
  <label for="tweet_status">Status</label>
  <input id="tweet_status" name="tweet[status]" size="30" type="text" />
 
  <label for="tweet_zombie_id">Zombie</label>
  <select id="tweet_zombie_id" name="tweet[zombie_id]">
    <option value="1">Ash</option>
    <option value="2">bob</option>
  </select>
 
  <input name="commit" type="submit" value="Create Tweet" />
 
</form>
复制代码

Answer:

复制代码
class CreatingATweetTest < ActionDispatch::IntegrationTest
  test 'should create a new tweet' do
    #visit new_tweet_url
    visit new_tweet_url
    #fill in the form, click the button
    fill_in "Status", with: "What's the hell?"
    select "Ash", from: "Zombie"
    click_button "Create Tweet"
    #check current path
    assert_equal current_path, tweet_path(Tweet.last)
  end
end
复制代码

 

CAPYBARA INTEGRATION TEST III

Now lets test that from the home page root_path we can click the New Tweet link and it brings us to the new_tweet_path.

 

class CreatingATweetTest < ActionDispatch::IntegrationTest
  test 'should go to new tweet page' do
    #tell where the hell you are
    visit root_url 
    click_on "New Tweet"
    assert_equal current_path, new_tweet_path
  end
end

 

HELPER METHODS

Refactor the tweet creation code of this test into its own helper method in test_helper.

复制代码
class CreatingATweetTest < ActionDispatch::IntegrationTest
  test 'should create a new tweet' do
    visit new_tweet_url
    fill_in 'Status', with: 'I love the way your brain feels'
    select 'Ash', from: 'Zombie'
    click_button 'Create Tweet'

    assert_equal tweet_path(Tweet.last), current_path
  end
end
复制代码

Answer:

creating_a_tweet_test.rb

class CreatingATweetTest < ActionDispatch::IntegrationTest
  test 'should create a new tweet' do
    create_tweet_for 'Ash', 'What\'s the hell with this?'
    assert_equal tweet_path(Tweet.last), current_path
  end
end

test_helper.rb

class ActiveSupport::TestCase
  def create_tweet_for zombie, status
    visit new_tweet_url
    fill_in 'Status', with: status
    select zombie, from: 'Zombie'
    click_button 'Create Tweet'
  end
end

 

posted @   Zhentiw  阅读(353)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示