标签: rspec-rails

如何使用 RSpec 测试 Rails 3.2 ActionMailer 正在渲染正确的视图模板?

我正在使用rspec-rails,我想测试我的邮件程序是否正在渲染正确的视图模板。

describe MyMailer do
  describe '#notify_customer' do
    it 'sends a notification' do
      # fire
      email = MyMailer.notify_customer.deliver

      expect(ActionMailer::Base.deliveries).not_to be_empty
      expect(email.from).to include "cs@mycompany.com"

      # I would like to test here something like
      # ***** HOW ? *****
      expect(template_path).to eq("mailers/my_mailer/notify_customer")
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

这是一个有效的方法吗?或者我应该做一些完全不同的事情?

更新

MyMailer#notify_customer可能有一些逻辑(例如,根据客户的区域设置)在不同情况下选择不同的模板。这或多或少与控制器在不同情况下渲染不同视图模板的问题类似。有了RSpec你就可以写

expect(response).to render_template "....." 
Run Code Online (Sandbox Code Playgroud)

它有效。我正在为邮寄者寻找类似的东西。

rspec ruby-on-rails actionmailer rspec-rails

5
推荐指数
1
解决办法
1299
查看次数

如何让水豚通过标签选中复选框

水豚没有找到我的复选框的标签,而且我知道我通过它的标签正确引用了它。我做错了什么,还是这是水豚的一个错误?

根据http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Node/Actions:check,“可以通过名称、ID 或标签文本找到该复选框。”

这是我运行的请求规范的部分:

describe "with valid information" do   

it_should_behave_like "all item pages"

before { valid_create_item }

it "should create an item" do
    expect { click_button submit }.to change(Item, :count).by(1)
end

describe "after saving the item" do
    before { click_button submit }

    it { should have_link('Sign out') }
    it { should have_selector('h1', text: "Items") }
    it { should have_title("Items") }
    it { should have_success_message }
end      

describe "and options selected" do

    before do
        puts page.html
        check('Option 1')
        click_button …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails capybara rspec-rails

5
推荐指数
1
解决办法
1万
查看次数

如何在 RSpec 视图示例中设置区域设置

我想测试视图以确保正确呈现错误消息。我的config.default_locale'fr'。因此,我希望我的视图能够从我的法语区域设置文件中找到正确的 Active Record 错误消息。

describe 'book/new.html.erb' do
  let(:subject) { rendered }
  before do
    @book = Book.create #this generates errors on my model
    render
  end
  it { should match 'some error message in French' }
end
Run Code Online (Sandbox Code Playgroud)

当单独运行或与其他规范/视图一起运行时,此测试通过。但是当我运行完整的测试套件时,视图会呈现以下消息:translation missing: en.activerecord.errors.models.book.attributes.title.blank

我不明白为什么它会以en语言环境呈现。我尝试使用以下命令强制区域设置:

before do
  allow(I18n).to receive(:locale).and_return(:fr)
  allow(I18n).to receive(:default_locale).and_return(:fr)
end
Run Code Online (Sandbox Code Playgroud)

before do
  default_url_options[:locale] = 'fr'
end
Run Code Online (Sandbox Code Playgroud)

有人有想法吗?

ruby testing rspec ruby-on-rails rspec-rails

5
推荐指数
1
解决办法
2248
查看次数

RSpec 功能规范找不到 Rails 引擎的路由

我正在使用rails v5.1.0和开发Rails 引擎rspec-rails 3.5.2

我有一个简单的功能规范:

require "rails_helper"

module MyEngineName
  RSpec.feature "Some Feature", type: :feature do
    it "user can navigate to page and blah blah", :js do
      visit edit_job_path(1)
      # .... other stuff
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

这引发了错误

undefined method `edit_job_path' for #<RSpec::ExampleGroups::SomeFeature:0x007fc098a570e8>
Run Code Online (Sandbox Code Playgroud)

因为edit_job_path找不到路由助手。

为了让我的功能规范能够访问我的引擎路线,我需要做什么特别的事情吗?

RSpec 文档提到您可以指定引擎 routes,但这似乎仅适用于路由规范。当我将它添加到功能规格时,它失败了undefined method 'routes'

谢谢!

编辑:由于请求了我的路由文件,因此将其添加到此处。很短——

# config/routes.rb
MyEngineName::Engine.routes.draw do
  root to: redirect("/my_engine_name/jobs")
  resources :jobs
end
Run Code Online (Sandbox Code Playgroud)

来自 rake 的所有路由列表

> rake app:routes
   ....
   ....

Routes for …
Run Code Online (Sandbox Code Playgroud)

rspec ruby-on-rails rails-engines rspec-rails

5
推荐指数
1
解决办法
1538
查看次数

Rails RSpec、DRY 规范:共享示例与辅助方法与自定义匹配器

我对控制器规范中的每个 HTTP 方法/控制器操作组合重复了一次以下测试:

it "requires authentication" do
  get :show, id: project.id
  # Unauthenticated users should be redirected to the login page
  expect(response).to redirect_to new_user_session_path
end
Run Code Online (Sandbox Code Playgroud)

我找到了以下三种方法来重构它并消除重复。哪一个最合适?

共享示例

在我看来,共享示例是最合适的解决方案。但是,为了将 传递params给共享示例而必须使用块感觉有点尴尬。

shared_examples "requires authentication" do |http_method, action|
  it "requires authentication" do
    process(action, http_method.to_s, params)
    expect(response).to redirect_to new_user_session_path
  end
end

RSpec.describe ProjectsController, type: :controller do
  describe "GET show", :focus do
    let(:project) { Project.create(name: "Project Rigpa") }

    include_examples "requires authentication", :GET, :show do
      let(:params) { {id: project.id} }
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

辅助方法 …

ruby rspec ruby-on-rails rspec-rails

5
推荐指数
1
解决办法
1177
查看次数

rails 5 水豚 test.log

test.log在进行一些测试时遇到了问题,我想在请求中看到正在完成的请求。我可以更改配置以在日志中注册请求,如下所示?

Started GET "/carts" for 127.0.0.1 at 2017-07-25 19:09:55 -0300
  Processing by CartsController#show as HTML
  Rendering carts/show.html.erb within layouts/application
  Rendered layouts/_header.html.erb (1.5ms)
  Rendered layouts/_notice_modal.html.erb (1.2ms)
  Rendered carts/show.html.erb within layouts/application (8.9ms)
  Rendered layouts/_alert_modal.html.erb (0.7ms)
  Rendered layouts/_notice_modal.html.erb (0.4ms)
Completed 200 OK in 62ms (Views: 57.6ms | ActiveRecord: 0.0ms)
Run Code Online (Sandbox Code Playgroud)

我使用的是 Rails 5.1.1、RSpec 3.6 和 Capybara 2.14.4。

在此先感谢您的帮助!

编辑 1

我在水豚中有以下配置rails_helper.rb

require "capybara"
require "capybara/rspec"

....

Capybara.register_driver :selenium_chrome do |app|
  Capybara::Selenium::Driver.new(app, browser: :chrome)
end

Capybara.javascript_driver = :selenium_chrome
Run Code Online (Sandbox Code Playgroud)

testing capybara rspec-rails ruby-on-rails-5

5
推荐指数
0
解决办法
401
查看次数

Rspec 3.7.0 在规范套件中显示 Puma 日志

我刚刚将 Rspec 从 3.6.0 升级到 3.7.0,将 rspec-rails 从 3.6.1 升级到 3.7.1。我在 rails 5.1 应用程序中使用这些库。

自从这次升级以来,这发生在我的规范套件中:

在此处输入图片说明

它没有链接到特定的规格,它显示在随机位置。

看起来 rspec 配置中发生了一些变化,但我在更改日志中找不到它。

rspec ruby-on-rails rspec-rails

5
推荐指数
1
解决办法
312
查看次数

ArgumentError:SMTP 收件人地址不能为空:[]

在这里有点困惑,我正在我的 rails 应用程序上运行 rspec 测试并有一个模型规范:

it { should validate_uniqueness_of(:email).case_insensitive }
Run Code Online (Sandbox Code Playgroud)

这是电子邮件对象的 1 of3 测试,它不断失败并出现以下错误:

1) User should validate that :email is case-insensitively unique
 Failure/Error: it { should validate_uniqueness_of(:email).case_insensitive }

 ArgumentError:
   SMTP To address may not be blank: []
 # ./spec/models/user_spec.rb:43:in `block (2 levels) in <top (required)>'
Run Code Online (Sandbox Code Playgroud)

我不明白为什么在模型中测试该电子邮件地址需要任何 SMTP TO 地址。

不知道看什么帮助会很好,因为这是我唯一失败的测试:(

只有我认为可能有帮助的其他东西是我运行:

  • 红宝石 v2.5.0p0
  • 导轨 v5.1.6
  • RSpec 3.7
    • rspec 核心 3.7.1
    • rspec-期望 3.7.0
    • rspec 模拟 3.7.0
    • rspec-rails 3.7.2
    • rspec 支持 3.7.1

ruby rspec ruby-on-rails rspec-rails

5
推荐指数
1
解决办法
4841
查看次数

rspec rails 6 控制器

我使用 rails 6 创建了一个新项目,但我无法使用 rspec 3.8 或 3.9.0.pre 测试许多控制器,例如这个测试:

it 'OK' do
  get :index
  expect(response).to be_ok
end
Run Code Online (Sandbox Code Playgroud)

加注

Failure/Error: render template: 'rig_masters/index'

     ActionView::Template::Error:
       wrong number of arguments (given 2, expected 1)
Run Code Online (Sandbox Code Playgroud)

如果我有一个呈现json的控制器,它会通过,例如,如果控制器是

Failure/Error: render template: 'rig_masters/index'

     ActionView::Template::Error:
       wrong number of arguments (given 2, expected 1)
Run Code Online (Sandbox Code Playgroud)

测试通过

但是如果我尝试渲染像

def index
  @components = Component.recent
  render json: @components
end
Run Code Online (Sandbox Code Playgroud)

甚至

def index
  @components = Component.recent
end
Run Code Online (Sandbox Code Playgroud)

引发ActionView::Template::Error: wrong number of arguments (given 2, expected 1)错误

任何使这些测试通过的帮助将不胜感激。

ruby-on-rails rspec-rails ruby-on-rails-6

5
推荐指数
2
解决办法
2153
查看次数

在 rails 6 中使用带有 rspec 的防护显示警告

运行时bundle exec guard收到这些警告。

<main>:1: warning: __FILE__ in eval may not return location in binding; use Binding#source_location instead
/home/workstation/.rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/gems/pry-0.12.2/lib/pry/commands/whereami.rb:40: warning: in `eval'
<main>:1: warning: __LINE__ in eval may not return location in binding; use Binding#source_location instead
/home/workstation/.rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/gems/pry-0.12.2/lib/pry/commands/whereami.rb:41: warning: in `eval'
<main>:1: warning: __FILE__ in eval may not return location in binding; use Binding#source_location instead
/home/workstation/.rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/gems/pry-0.12.2/lib/pry/method/weird_method_locator.rb:88: warning: in `eval'
<main>:1: warning: __FILE__ in eval may not return location in binding; use Binding#source_location instead
/home/workstation/.rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/gems/pry-0.12.2/lib/pry/method/weird_method_locator.rb:80: warning: in `eval'
Run Code Online (Sandbox Code Playgroud)

它显然来自 pry gem …

ruby ruby-on-rails guard rspec-rails

5
推荐指数
1
解决办法
1123
查看次数