标签: rspec-rails

测试“验证...,格式=> 与”

我有一个带有此验证的用户模型

validates :name, :lastname, :format => {:with => /^[a-zA-Z]+$/, :message => 'Only letters and spaces allowed.'}
Run Code Online (Sandbox Code Playgroud)

我不确定如何正确测试它。

我已经完成了一个函数,该函数返回由 a-zA-z 字符数组中的 10 个字符组成的随机字符串。

def get_random_name
  "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".split('').shuffle[0..10].join
Run Code Online (Sandbox Code Playgroud)

结束 结束

然后我为每次运行的规格获得一个新名称。

我不想测试它是否使用了一些正则表达式,因为那样我将测试实现而不是行为,而且我也不想只测试一个硬编码案例。

我的问题是:我应该这样做吗?真的需要吗?它是更好还是无用?您知道测试这种验证的更好方法吗?

编辑:另一个问题,如何生成无效的随机名称?有没有办法创建包含至少一个超出允许值的字符的随机名称?我不能硬编码所有无效值的数组来随机化它,因为它太大了

rspec rspec-rails

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

NameError:未定义的局部变量或方法`app'

我创建的控制器规范失败,并显示以下错误消息:

NameError: undefined local variable or method `app' for \
  #<RSpec::Core::ExampleGroup::Nested_1::Nested_1::Nested_1:0x00000004424880>
Run Code Online (Sandbox Code Playgroud)

...

# spec/controllers/sessions_conroller_spec.rb
require 'spec_helper'

describe SessionsController do

  before do
    @user = User.gen!
  end

  describe "#create" do
    context "when sending valid email and password" do
      it "renders a json hash ..." do
        post :create, email: @user.email, password: @user.password
        expect(last_response.status).to eq(201)
      end
    end
  end

  describe "#destroy" do
    context "when sending valid email and authentication token" do
      it "renders a json hash ..." do
        delete :destroy, email: @user.email, auth_token: @user.authentication_token
        expect(last_response.status).to …
Run Code Online (Sandbox Code Playgroud)

rack rspec ruby-on-rails rspec-rails

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

Rails和RSpec:`rake spec`运行所有规格两次

我使用RSpec设置了一个新的Rails 4应用程序.但是在运行时rake rspec,所有示例都运行了两次:

rake spec
[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.
/Users/josh/.rvm/rubies/ruby-2.0.0-p353/bin/ruby -S rspec ./spec/controllers/dashboards_controller_spec.rb ./spec/models/member_spec.rb ./spec/requests/members_spec.rb ./spec/routing/members_routing_spec.rb
[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.
 11/11 |============================================ 100 ============================================>| Time: 00:00:00 

Finished in …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails rspec-rails

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

在Rails + RSpec中对String对象使用have_selector

我在lib /目录下的文件上写了一个规范.我在那里的一个方法返回一个HTML字符串,但后来我尝试了类似的东西:

expect(subject).to have_selector("p:eq(1)") do |first_p|
Run Code Online (Sandbox Code Playgroud)

它给了我以下错误:

Failure/Error: expect(subject).to have_selector("p:eq(1)") do |first_p|
NoMethodError:
  undefined method `has_selector?' for #<String:0x007f93798a8338>
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个问题?我应该用另一个特定对象包装字符串对象吗?我试过Nokogiri :: HTML.fragment(),但它也没有用.我想我应该用一些webrat的对象或类似的东西包装它,但我不知道如何到达那里.提前致谢.

rspec ruby-on-rails rspec-rails

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

使用rspec进行文件上传测试

如何生成用于测试的文件,并在 rspec 中上传给定大小和测试文件?我想生成文件。

我目前在 fixtures/files 中有文件,并使用 fixture_file_upload 作为测试文件。我想消除对该示例文件的需求。

specifications rspec ruby-on-rails rspec-rails

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

Rspec 测试 API 版本控制

我正在使用 rails 4.1.8 中的 web api,我正在做一些测试,但总是让我返回

Failure/Error: post '/v1/users', subdomain: 'api', user: @user.to_json, format: :json
 ActionController::UrlGenerationError:
   No route matches {:action=>"/v1/users", :controller=>"api/v1/users", :format=>:json, :subdomain=>"api", :user=>"JSON_OBJ"}
Run Code Online (Sandbox Code Playgroud)

但是如果我使用 Advance Rest Console 在浏览器中测试它,它就可以工作,我不知道我做错了什么

这是我的代码

路由文件

namespace :api, path: '/', constraints: {subdomain: 'api'}, defaults: { format: :json } do
  namespace :v1 do
    with_options except: [:edit, :new] do |except|
      except.resources :users do
        collection do
          post 'login'
          post 'showme'
        end
      end
      except.resources :products
      except.resources :locations
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

和我的控制器规格

module API
  module V1
    describe UsersController do
     before …
Run Code Online (Sandbox Code Playgroud)

ruby rspec ruby-on-rails rspec-rails ruby-on-rails-4

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

使用 RSpec 验证属性的存在

我的模型中的某些属性具有存在验证,我想在我的规范中添加测试以检查当属性为空时是否生成错误。

我正在使用此代码:

it 'should have a name' do
    expect(@patient.errors[:name].size).to eq(1)
end
Run Code Online (Sandbox Code Playgroud)

但这是 rspec 命令的结果:

失败:

  1) 患者应有姓名
     失败/错误:expect(@patient.errors[:name].size).to eq(1)

       预期:1
            得到:0

       (比较使用 ==)
     # ./spec/models/patient_spec.rb:11:in `block (2 levels) in '

在 0.03002 秒内完成(文件需要 40.54 秒加载)
1个例子,1个失败

失败的例子:

rspec ./spec/models/patient_spec.rb:10 # 患者应该有一个名字

rspec-rails rspec3

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

rspec给出的错误`Then`在示例中不可用(例如`it`块)

我正在使用rspec-given并继续收到此错误.

故障/错误:然后{ Then从一个示例(例如,内不可用it块)或者从在一个例子(例如范围运行构建体before,let等).它仅适用于示例组(例如a describecontext块).

describe SchoolService do
  Given(:school) { create(:school_with_applications) }
  Given(:service) { School.new(@school) }

  describe 'create_default_programs_and_year_grades!' do
    it 'checks program size' do 
      When { service.create_default_programs_and_year_grades! }
      Then { expect(school.programs.size).to eq 3 }
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

ruby rspec ruby-on-rails rspec-rails ruby-on-rails-4

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

使用 rspec 运行 Rails-tutorial 时 nil:NilClass 的未定义方法“文档”

我正在关注railstutorial by Michael Hartl,但我不明白第 5 章中测试失败的原因。这本书使用了 minitest 框架,但我决定使用 RSpec。为此,我删除了 test 文件夹并将其包含rspec-rails在我的 Gemfile 中,然后运行 ​​bundle install 并rails g rspec:install生成我的 spec 文件夹。但是,我觉得使用 minitest 语法运行一些测试很方便,例如assert_select在static_pages_controller_spec.rb文件中。这是我的规范文件的样子:

require "rails_helper"

RSpec.describe StaticPagesController, type: :controller do
  describe "GET #home" do
    it "returns http success" do
      get :home
      expect(response).to have_http_status(:success)
    end
    it "should have the right title" do
      get :home
      assert_select "title", "Ruby on Rails Tutorial Sample App"
    end
  end

  describe "GET #help" do
    it "returns http success" …
Run Code Online (Sandbox Code Playgroud)

minitest rspec-rails railstutorial.org ruby-on-rails-4

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

Capybara :: ElementNotFound:无法找到可见字段

我的Capybara测试终端收到以下错误:

Failures:

  1) Users User create new user
     Failure/Error: fill_in 'Username', with: 'Ali', visible: false

     Capybara::ElementNotFound:
       Unable to find field "Username" that is not disabled within #<Capybara::Node::Element tag="form" path="/html/body/form">
     # ./spec/features/users_spec.rb:31:in `block (4 levels) in <top (required)>'
     # ./spec/features/users_spec.rb:30:in `block (3 levels) in <top (required)>
Run Code Online (Sandbox Code Playgroud)

我在水豚的测试代码:

require 'rails_helper'
RSpec.feature "Users", type: :feature do
  describe "User", :type => :feature do
    it "create new user" do
      visit '/signup'
      within('form') do
        fill_in 'Username', with: 'Ali', visible: false
        fill_in 'Password', with: 'ali', visible: false …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails capybara rspec-rails

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