【问题标题】:using Rspec and internationalization with nice routing (current_path.should == new_post_path )使用具有良好路由的 Rspec 和国际化 (current_path.should == new_post_path)
【发布时间】:2012-08-21 19:06:14
【问题描述】:

我正在使用 Rails、Rspec(用于测试)和 capybara 创建一个多语言 CMS。

我写这个测试时出现的问题

it "redirect and create a new post" do
  visit posts_path
  create_new_post = I18n.t('posts.index.create_new_post')
  click_link create_new_post
  current_path.should == new_post_path
end

问题在最后一行,我明白了 预期:“/posts/new” 得到:“/en/posts/new”(使用 ==)

我想使用类似“new_post_path”的东西,而不是“/en/posts/new”

当我使用这样的东西时描述的主要问题

it "redirect to show link" do
  visit posts_path
  page.should have_content 'show'
  click_link 'show'

  current_path.should == post_path(@post)
end

感谢任何帮助。

【问题讨论】:

    标签: ruby-on-rails internationalization rspec rails-routing


    【解决方案1】:

    查看this StackOverflow question 了解有关此主题的一些想法。对于同一个问题,我的首选方式是this answer。但是,使用你当前的代码,为了让你的期望正确,你可能会做这样的事情(假设你想测试你所有的语言环境):

    I18n.available_locales.each do |locale|
    
      it "should redirect and create a new post" do
        visit posts_path(locale)
        create_new_post = I18n.t('posts.index.create_new_post')
        click_link create_new_post
        current_path.should == new_post_path(locale)
      end
    
      it "should redirect to show link" do
        visit posts_path(locale)
        page.should have_content 'show'
        click_link 'show'
    
        current_path.should == post_path(locale, @post)
      end
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-26
      • 1970-01-01
      • 1970-01-01
      • 2011-09-07
      • 2013-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多