- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 Controller 测试中得到了一个未定义的“应该”方法,但无法弄清楚原因。我花了一些时间在谷歌和堆栈溢出上,但被卡住了。有什么帮助吗?
规范助手:
require 'simplecov'
SimpleCov.start 'rails'
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
config.expect_with :rspec do |c|
c.syntax = :expect
end
# ## Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#
#config.mock_with :mocha
# config.mock_with :flexmock
# config.mock_with :rr
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = false
# If true, the base class of anonymous controllers will be inferred
# automatically. This will be the default behavior in future versions of
# rspec-rails.
config.infer_base_class_for_anonymous_controllers = false
# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = "random"
end
Capybara.javascript_driver = :webkit
require 'spec_helper'
include Devise::TestHelpers
describe PagesController do
# This should return the minimal set of attributes required to create a valid
# Page. As you add validations to Page, be sure to
# update the return value of this method accordingly.
def valid_attributes
{ }
end
# This should return the minimal set of values that should be in the session
# in order to pass any filters (e.g. authentication) defined in
# PagesController. Be sure to keep this updated too.
def valid_session
{}
end
describe "GET index" do
it "assigns all pages as @pages" do
page = Page.create! valid_attributes
get :index, {}, valid_session
assigns(:pages).should eq([page])
end
end
describe "GET show" do
it "assigns the requested page as @page" do
page = Page.create! valid_attributes
get :show, {:id => page.to_param}, valid_session
assigns(:page).should eq(page)
end
end
describe "GET new" do
it "assigns a new page as @page" do
get :new, {}, valid_session
assigns(:page).should be_a_new(Page)
end
end
describe "GET edit" do
it "assigns the requested page as @page" do
page = Page.create! valid_attributes
get :edit, {:id => page.to_param}, valid_session
assigns(:page).should eq(page)
end
end
describe "POST create" do
describe "with valid params" do
it "creates a new Page" do
expect {
post :create, {:page => valid_attributes}, valid_session
}.to change(Page, :count).by(1)
end
it "assigns a newly created page as @page" do
post :create, {:page => valid_attributes}, valid_session
assigns(:page).should be_a(Page)
assigns(:page).should be_persisted
end
it "redirects to the created page" do
post :create, {:page => valid_attributes}, valid_session
response.should redirect_to(Page.last)
end
end
describe "with invalid params" do
it "assigns a newly created but unsaved page as @page" do
# Trigger the behavior that occurs when invalid params are submitted
Page.any_instance.stub(:save).and_return(false)
post :create, {:page => { }}, valid_session
assigns(:page).should be_a_new(Page)
end
it "re-renders the 'new' template" do
# Trigger the behavior that occurs when invalid params are submitted
Page.any_instance.stub(:save).and_return(false)
post :create, {:page => { }}, valid_session
response.should render_template("new")
end
end
end
describe "PUT update" do
describe "with valid params" do
it "updates the requested page" do
page = Page.create! valid_attributes
# Assuming there are no other pages in the database, this
# specifies that the Page created on the previous line
# receives the :update_attributes message with whatever params are
# submitted in the request.
Page.any_instance.should_receive(:update_attributes).with({ "these" => "params" })
put :update, {:id => page.to_param, :page => { "these" => "params" }}, valid_session
end
it "assigns the requested page as @page" do
page = Page.create! valid_attributes
put :update, {:id => page.to_param, :page => valid_attributes}, valid_session
assigns(:page).should eq(page)
end
it "redirects to the page" do
page = Page.create! valid_attributes
put :update, {:id => page.to_param, :page => valid_attributes}, valid_session
response.should redirect_to(page)
end
end
describe "with invalid params" do
it "assigns the page as @page" do
page = Page.create! valid_attributes
# Trigger the behavior that occurs when invalid params are submitted
Page.any_instance.stub(:save).and_return(false)
put :update, {:id => page.to_param, :page => { }}, valid_session
assigns(:page).should eq(page)
end
it "re-renders the 'edit' template" do
page = Page.create! valid_attributes
# Trigger the behavior that occurs when invalid params are submitted
Page.any_instance.stub(:save).and_return(false)
put :update, {:id => page.to_param, :page => { }}, valid_session
response.should render_template("edit")
end
end
end
describe "DELETE destroy" do
it "destroys the requested page" do
page = Page.create! valid_attributes
expect {
delete :destroy, {:id => page.to_param}, valid_session
}.to change(Page, :count).by(-1)
end
it "redirects to the pages list" do
page = Page.create! valid_attributes
delete :destroy, {:id => page.to_param}, valid_session
response.should redirect_to(pages_url)
end
end
end
1) PagesController GET index assigns all pages as @pages
Failure/Error: assigns(:pages).should eq([page])
NoMethodError:
undefined method `should' for #<Array:0x007fb986c42948>
# ./spec/controllers/pages_controller_spec.rb:23:in `block (3 levels) in <top (required)>'
2) PagesController POST create with valid params creates a new Page
Failure/Error: expect {
count should have been changed by 1, but was changed by 0
# ./spec/controllers/pages_controller_spec.rb:53:in `block (4 levels) in <top (required)>'
3) PagesController POST create with valid params assigns a newly created page as @page
Failure/Error: assigns(:page).should be_a(Page)
NoMethodError:
undefined method `should' for nil:NilClass
# ./spec/controllers/pages_controller_spec.rb:60:in `block (4 levels) in <top (required)>'
4) PagesController POST create with valid params redirects to the created page
Failure/Error: response.should redirect_to(Page.last)
NoMethodError:
undefined method `should' for #<ActionController::TestResponse:0x007fb987232c68>
# ./spec/controllers/pages_controller_spec.rb:66:in `block (4 levels) in <top (required)>'
5) PagesController POST create with invalid params assigns a newly created but unsaved page as @page
Failure/Error: assigns(:page).should be_a_new(Page)
NoMethodError:
undefined method `should' for nil:NilClass
# ./spec/controllers/pages_controller_spec.rb:75:in `block (4 levels) in <top (required)>'
6) PagesController POST create with invalid params re-renders the 'new' template
Failure/Error: response.should render_template("new")
NoMethodError:
undefined method `should' for #<ActionController::TestResponse:0x007fb986e1d1f0>
# ./spec/controllers/pages_controller_spec.rb:82:in `block (4 levels) in <top (required)>'
7) PagesController GET new assigns a new page as @page
Failure/Error: assigns(:page).should be_a_new(Page)
NoMethodError:
undefined method `should' for #<Page:0x007fb982404708>
# ./spec/controllers/pages_controller_spec.rb:38:in `block (3 levels) in <top (required)>'
8) PagesController PUT update with valid params updates the requested page
Failure/Error: put :update, {:id => page.to_param, :page => { "these" => "params" }}, valid_session
#<Page:0x007fb9872517f8> received :update_attributes with unexpected arguments
expected: ({"these"=>"params"})
got: ({})
# ./app/controllers/pages_controller.rb:61:in `block in update'
# ./app/controllers/pages_controller.rb:60:in `update'
# ./spec/controllers/pages_controller_spec.rb:96:in `block (4 levels) in <top (required)>'
9) PagesController PUT update with valid params assigns the requested page as @page
Failure/Error: assigns(:page).should eq(page)
NoMethodError:
undefined method `should' for #<Page:0x007fb981579a30>
# ./spec/controllers/pages_controller_spec.rb:102:in `block (4 levels) in <top (required)>'
10) PagesController PUT update with valid params redirects to the page
Failure/Error: response.should redirect_to(page)
NoMethodError:
undefined method `should' for #<ActionController::TestResponse:0x007fb9827b7350>
# ./spec/controllers/pages_controller_spec.rb:108:in `block (4 levels) in <top (required)>'
11) PagesController PUT update with invalid params assigns the page as @page
Failure/Error: assigns(:page).should eq(page)
NoMethodError:
undefined method `should' for #<Page:0x007fb987444a88>
# ./spec/controllers/pages_controller_spec.rb:118:in `block (4 levels) in <top (required)>'
12) PagesController PUT update with invalid params re-renders the 'edit' template
Failure/Error: response.should render_template("edit")
NoMethodError:
undefined method `should' for #<ActionController::TestResponse:0x007fb982e75750>
# ./spec/controllers/pages_controller_spec.rb:126:in `block (4 levels) in <top (required)>'
13) PagesController GET show assigns the requested page as @page
Failure/Error: assigns(:page).should eq(page)
NoMethodError:
undefined method `should' for #<Page:0x007fb9830f3a68>
# ./spec/controllers/pages_controller_spec.rb:31:in `block (3 levels) in <top (required)>'
14) PagesController DELETE destroy redirects to the pages list
Failure/Error: response.should redirect_to(pages_url)
NoMethodError:
undefined method `should' for #<ActionController::TestResponse:0x007fb988100e98>
# ./spec/controllers/pages_controller_spec.rb:142:in `block (3 levels) in <top (required)>'
15) PagesController GET edit assigns the requested page as @page
Failure/Error: assigns(:page).should eq(page)
NoMethodError:
undefined method `should' for #<Page:0x007fb9872585f8>
# ./spec/controllers/pages_controller_spec.rb:46:in `block (3 levels) in <top (required)>'
最佳答案
RSpec 提供了两种编写期望值的方法:
1.should == 1
expect(1).to eq(1)
c.syntax = :should
关于ruby-on-rails - RSpec 未定义方法 'should',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16064647/
我想知道 Rspec 中的命令式和声明式步骤是什么。 这是 Rspec 书中的示例代码: Scenario: transfer money (declarative) Given I have $10
我正在尝试使用 Travis 设置 CI。但是我遇到了在 Travis 上失败但在本地没有的测试,甚至提供了相同的种子。 我认为种子运行相同,但现在我不确定,并想弄清楚它是否存在,所以我现在在哪里看。
RSpec 的文档提到了 --bisect option ,当运行时提供最小的复制,例如 rspec ./spec/calculator_10_spec.rb[1:1] ./spec/calculat
我有一些昂贵的测试设置,仅对我的规范中的少数示例是必需的,如果需要,它只需要运行一次。因为它很慢,所以我试图避免将它放在 before(:each) block 中,但 before(:all) 似乎
在我的 Gemfile 中 gem 'rspec-rails', '~> 3.3' 我发现,errors_on 匹配器移动到了另一个 gem:https://github.com/rspec/rspe
如果我运行此命令“rspec ./spec/requests/api/v1/password_reset_request_spec.rb”,此文件中的所有测试都会通过。 但是,当我运行“rspec”时
我花了 4 天时间尝试正确安装 Ruby on Rails,但我遇到了一个大问题。我第一次使用Linux(对我来说更舒服),但遇到问题后,我使用了Windows。令我惊讶的是,我在 Windows 上
我在这样的验证中测试了很多坏字符串: ['0', '3:a', 'xx:11', '-1', '-3:00', 'h', 'h2', 'h2h', 'm', 'm10', 'm10m', '2hm',
我的测试套件中有一部分运行着一堆非常慢的导入器。这些测试不需要经常运行(除非实际上正在处理导入器)所以我使用 Rspec 标签将它们分开:http://relishapp.com/rspec/rspe
Rspec 尝试在运行任何规范文件或整个测试套件结束时运行 Test::Unit 测试。它仍然没关系,因为我没有任何测试单元测试文件,但它尝试传递给 rspec 的命令行选项,因为它们将传递给 Tes
我有一段代码要测试: class User :destroy end 请举例测试关联代码的RSpec代码。 最佳答案 对我来说,这个变体效果很好: describe User do it { s
我正在尝试在 rails Controller 规范中重用一些通用代码。我对管理员用户和普通用户有不同的上下文。但是,对于特定操作,大部分行为是相同的,因此我尝试将这种常见行为提取到辅助函数中: de
我有两个类OneClass和 AnotherClass : class OneClass def initialize(*args) @another_member = AnotherCl
我有一个看起来像这样的模型: class Gist def self.create(options) post_response = Faraday.post do |request|
我是 RSPEC 的新手。我编写了一个名为 result_spec.rb 的 RSPEC 代码,如下所示: describe '#grouped_scores' do subject { result
我对字符串类进行了如下扩展: class String def last_character self[-1] end end 我将 string.rb 文件放在 lib 中,如下所示
我是 RSpec 的新手,我在其中编写了一个测试场景: my_object.should_not be_valid 它工作正常。但是,我想测试模型的特定属性是否无效。这样的 RSpec 行为是现成的吗
我正在尝试测试邮政编码属性的长度以确保其长度为 5 个字符。现在我正在测试以确保它不是空白,然后是 4 个字符太短,6 个字符太长。 有没有办法测试它是否正好是 5 个字符?到目前为止,我在网上或 r
我将rspec与电子邮件规范gem一起使用。我正在尝试做: last_delivery = ActionMailer::Base.deliveries.last last_delivery.body.
我创建了一个新的 rails 应用程序,并按照此处的 rspec-rails 安装说明进行操作 - https://github.com/rspec/rspec-rails然后我在我的 app/lib
我是一名优秀的程序员,十分优秀!