标签: rspec-rails

what does this line of code "expect { |b| 5.tap(&b) }.to yield_control" implementing functionally?

Going through the rspec docs i found these lines of code in yielding section of this page http://rubydoc.info/gems/rspec-expectations/frames

任何人都可以解释屈服部分中每行代码的步骤

ruby ruby-on-rails rspec-rails

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

当预期输出正确时,Rspec has_selector错误

我有3个rspec选择器失败时,他们都应该成功.我跟随rails-tutorial.org书和他的节目一样正确.

PagesController GET 'home' should have the right title
     Failure/Error: response.should have_selector("title", :content => "Ruby on Rails     Sample App | Home")
       expected following output to contain a <title>Ruby on Rails Sample App | Home</title> tag:
   <!DOCTYPE html>
   <html>
   <head>
   <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
   <title>Ruby on Rails Tutorial Sample App | Home</title>
   </head>
Run Code Online (Sandbox Code Playgroud)

与'内容'和'约'完全相同的错误

application.html.erb

<!DOCTYPE html>
<html>
<head>
    <title><%= title %></title>
    <%= csrf_meta_tag %>
</head>
<body>
    <%= yield %>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

home.html.erb

    <h1>Sample App</h1>
    <p>
    This is the home page …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails capybara rspec-rails railstutorial.org

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

rspec核心配置错误:'load':无法加载此类文件

我是rails的新手,我正在学习一个教程.当我尝试使用RSpec测试示例应用程序时,我收到以下错误:

C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.14.4/lib/rspec/core/configuration.rb:896:in `load': 
cannot load such file -- 
C:/Sites/sample_app/spec/requests/static_pages.rb (LoadError)
Run Code Online (Sandbox Code Playgroud)

我已经安装了所需的gem,我bundle exec rspec spec\requests\static_pages.rb在我的应用程序的文件夹中运行命令.我的Gemfile如下:

source 'https://rubygems.org'

gem 'rails', '4.0.0.rc2'
group :development, :test do
  gem 'sqlite3'
  gem 'rspec-rails'
end
gem 'sass-rails', '~> 4.0.0.rc2'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 1.2'

group :doc do
  gem 'sdoc', require: false
end

group :test do
  gem 'capybara'
end
Run Code Online (Sandbox Code Playgroud)

我在Windows 7 Ultimate 64bit上运行.请帮助:)我在网上找不到解决方案..

ruby-on-rails rspec-rails

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

新的RSpec运行但从未进入模型

我目前正在使用rails 4.1.1和ruby 2.1.1运行rspec但是我得到了一堆与gems有关的错误,甚至没有进入模型.

这是一个新版本的rspec,它有一个spec_helper.rb和一个rails_helper.rb

您需要使用该文件中的rails_helper.rb随后引用spec_helper.rb

我没有对这两个帮助文件进行任何更改,只是刚开始使用我的项目.任何帮助/信息将不胜感激!

$ bundle exec rspec
    /.rvm/gems/ruby-2.1.1/gems/simplecov-html-0.8.0/lib/simplecov-html.rb:58: warning: possibly useless use of a variable in void context
    /.rvm/gems/ruby-2.1.1/gems/gibbon-1.1.3/lib/gibbon/api_category.rb:75: warning: method redefined; discarding old api_key=
    /.rvm/gems/ruby-2.1.1/gems/rack-cors-0.2.9/lib/rack/cors.rb:71: warning: shadowing outer local variable - logger
    /.rvm/gems/ruby-2.1.1/gems/rack-cors-0.2.9/lib/rack/cors.rb:174: warning: assigned but unused variable - x_origin
    /.rvm/gems/ruby-2.1.1/gems/stripe-1.14.0/lib/stripe.rb:172: warning: assigned but unused variable - ex
    /.rvm/gems/ruby-2.1.1/gems/rest-client-1.6.7/lib/restclient/exceptions.rb:157: warning: assigned but unused variable - message
    /.rvm/gems/ruby-2.1.1/gems/rest-client-1.6.7/lib/restclient/exceptions.rb:167: warning: assigned but unused variable - message
    /.rvm/gems/ruby-2.1.1/gems/rest-client-1.6.7/lib/restclient/response.rb:11: warning: method redefined; discarding old body
    /.rvm/gems/ruby-2.1.1/gems/rest-client-1.6.7/lib/restclient/payload.rb:47: warning: mismatched indentations at …
Run Code Online (Sandbox Code Playgroud)

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

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

RSpec NoMethodError:未定义的方法`assign'

我正在尝试为我为 Rails 编写的一个小 gem 编写测试。我想独立于一个虚拟应用程序测试 gem 并且觉得这对我来说是一个很好的学习体验。但是,由于上述错误,我坚持要通过一个简单的测试。

我的 Gemfile 指向 gemspec,我用rspec --init.

我的测试(只是为了确保在我开始使用 ernest 之前一切设置正确):

RSpec.describe 'text_field', type: :view do
    it 'has something on the page' do
        assign(:users, [
          mock_model("User", id: 1, name: 'Ashley')
        ])
        stub_template 'users/_form.html.erb' => "<%= form_for @user do |f| %><%= f.text_field :name' %><% end %>"
        render
        rendered.should =~ /Ashley/
    end
end
Run Code Online (Sandbox Code Playgroud)

整个错误:

text_field
  has something on the page (FAILED - 1)

Failures:

  1) text_field has something on the page
     Failure/Error: assign(:users, [
     NoMethodError:
       undefined …
Run Code Online (Sandbox Code Playgroud)

gem ruby-on-rails rspec-rails nomethoderror

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

Ruby on Rails 4.2.0 with rspec-rails 3.1.0 and shoulda-matchers 2.7.0 undefined method`validates_presence_of'for

我正在使用Ruby on Rails 4.2.0rspec-rails 3.1.0以及应用匹配器2.7.0

当运行测试我有这个错误

Failure/Error: it { should validates_presence_of :name }
     NoMethodError:
       undefined method `validates_presence_of' for #<RSpec::ExampleGroups::Profile::Vaidations:0x00000007881768>
     # ./spec/models/profile_spec.rb:5:in `block (3 levels) in <top (required)>'
Run Code Online (Sandbox Code Playgroud)

这是我的模特

class Profile < ActiveRecord::Base
  belongs_to :user
  accepts_nested_attributes_for :user
  validates :user, presence: true
  validates :username, presence: true, uniqueness: { case_sensitive: false }, length: {maximum: 50}
  validates :name, presence: true, length: {maximum: 50}
  validates :phone, presence: true, uniqueness: { case_sensitive: false }, length: {maximum: 50}
  validates :age, presence: true, …
Run Code Online (Sandbox Code Playgroud)

rspec ruby-on-rails shoulda rspec-rails

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

如何测试服务对象方法的调用?

我正在尝试为我的服务对象建立一些测试。

我的服务文件如下...

class ExampleService

  def initialize(location)
    @location = coordinates(location)
  end

  private

  def coordinates(location)
    Address.locate(location)
  end

end
Run Code Online (Sandbox Code Playgroud)

我想测试私有方法是否由公共方法调用。这是我的代码...

subject { ExampleService.new("London") }

it "receives location" do
  expect(subject).to receive(:coordinates)
  subject
end
Run Code Online (Sandbox Code Playgroud)

但是我得到这个错误...

expected: 1 time with any arguments
received: 0 times with any arguments
Run Code Online (Sandbox Code Playgroud)

rspec-rails

1
推荐指数
2
解决办法
3056
查看次数

Rspec allow_any_instance_of返回实例ID

有可能做这样的事情吗???

allow_any_instance_of(Object).to receive(:foo).and_return("hello #{instance.id}")
Run Code Online (Sandbox Code Playgroud)

我可以根据实例返回消息吗?

ruby rspec ruby-on-rails rspec-rails ruby-on-rails-3

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

rspec控制器测试:“预期#count更改为-1,但更改为0”

无法解决此错误。我不确定controller_spec的哪一部分写错了。请帮忙!

路线

Rails.application.routes.draw do
  resources :cases, only: [:index, :show, :new, :create, :edit, :update] do
    resources :links, only: [:create, :destroy]
  end
end
Run Code Online (Sandbox Code Playgroud)

控制者

class LinksController < ApplicationController

  before_action :prepare_case

  def destroy
    @link = @case_file.links.find(params[:id])
    @link.destroy
    redirect_to case_path(@case_file)
  end

  private

  def prepare_case
    @case_file = CaseFile.find(params[:case_id])
  end
end
Run Code Online (Sandbox Code Playgroud)

规格/工厂

FactoryGirl.define do

  factory :link do

    case_file
    url 'www.google.com'

    trait :invalid do
      case_file nil
      url ''
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

规格/控制器

require 'rails_helper'

RSpec.describe LinksController, type: :controller do

  let(:user) { build(:user) }
  before { login_user user …
Run Code Online (Sandbox Code Playgroud)

testing rspec ruby-on-rails rspec-rails

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

预期响应的状态码为200,但在rspec的请求测试中为302

我发现其他人发布了许多类似的问题,但是没有一个解决方案起作用。

我的/spec/requests/questions_spec.rb是

require 'rails_helper'
RSpec.describe "Questions", type: :request do
  describe "GET /questions" do
    it "works! (now write some real specs)" do
      get questions_path
      expect(response).to have_http_status(200)
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

现在我的rspec错误

Questions GET /questions works! (now write some real specs)
 Failure/Error: expect(response).to have_http_status(200)
   expected the response to have status code 200 but it was 302
 # ./spec/requests/questions_spec.rb:7:in `block (3 levels) in <top (required)>'
Run Code Online (Sandbox Code Playgroud)

谁能帮我?如何自定义代码以获得状态码200?

rspec ruby-on-rails rspec-rails

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