行动邮递员
def welcome_send(user)
@user = user
mail to: user.email, subject: 'Welcome to my site', from: 'suhasmv29@gmail.com'
end
Run Code Online (Sandbox Code Playgroud)
用于验证邮件已发送的 RSpec 测试用例
it "sends a confirmation email" do
expect { mail.to }.to change { ActionMailer::Base.deliveries.count }.by(1)
end
Run Code Online (Sandbox Code Playgroud)
错误
expected `ActionMailer::Base.deliveries.count` to have changed by 1, but was changed by 0
Run Code Online (Sandbox Code Playgroud) rspec ruby-on-rails actionmailer rspec-rails ruby-on-rails-4
我有回调名称authenticate_admin_user。如果当前用户电子邮件 ID 为“adminuser@admin.com”,则返回 true
def authenticate_admin_user!
if Current.user.blank? || Current.user.email != "adminuser@admin.com"
redirect_to courses_path, danger: "Admin login required"
end
end
Run Code Online (Sandbox Code Playgroud)
当我调用 new action 时,此回调将运行。在测试新操作时,我必须设置 Current.user 值
describe "GET /new" do
describe "Create course with admin login"
it "will not renders a new course page" do
get new_course_url
expect(response).to_not be_successful
end
it "will render a new course page" do
get new_course_url
expect(response).to be_successful
end
end
Run Code Online (Sandbox Code Playgroud)
如何设置Current.user的值?PS:这里 Current 是一个 ActiveSupport::Currentattributes 模型,而 user 是 Current 类中的一个属性。
我们可以检查enqueued_jobs.length,前提是电子邮件是唯一可能的后台作业类型。
it 'sends exactly one, particular email' do
expect { post :create }.to(
# First, we can check about the particular email we care about.
have_enqueued_mail(MyMailer, :my_particular_email)
)
# But we also have to check that no other email was sent.
expect(ActiveJob::Base.queue_adapter.enqueued_jobs.length).to eq(1)
end
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来断言:
MyMailer.my_particular_email已排队,我正在做第10章的Michael Hartl的Rails Tutorial截屏视频+在线书籍,我被困在Destroying User的最后一章子.
控制台输出:
Failures:
1) UsersController DELETE 'destroy' as an admin user should destroy the user
Failure/Error: delete :destroy, :id => @user
NameError:
undefined local variable or method `users' for #<UsersController:0x00000104a2f698>
./app/controllers/users_controller.rb:47:in `destroy'
./spec/controllers/users_controller_spec.rb:321:in `block (5 levels) in <top (required)>'
./spec/controllers/users_controller_spec.rb:320:in `block (4 levels) in <top (required)>'
2) UsersController DELETE 'destroy' as an admin user should redirect to the users page
Failure/Error: delete :destroy, :id => @user
NameError:
undefined local variable or method `users' for #<UsersController:0x000001049bf370>
./app/controllers/users_controller.rb:47:in `destroy' …Run Code Online (Sandbox Code Playgroud) 我是新手在Rails应用程序中使用RSpec而我无法让RSpec看到我的模型:(
我在我的文章中添加了以下几行 Gemfile
group :development, :test do
gem 'rspec-rails'
end
Run Code Online (Sandbox Code Playgroud)
并安装它
rails generate rspec::install
Run Code Online (Sandbox Code Playgroud)
然后我facebook_page.rb在app/models/目录中创建了一个文件,它不是 ActiveModel
class FacebookPage
class << self
# define class variables
attr_accessor :app_id
attr_accessor :app_secret
end
end
Run Code Online (Sandbox Code Playgroud)
现在我正在尝试测试它,所以我添加了一个facebook_page_spec.rbin spec/models/目录
describe FacebookPage do
describe ".app_id" do
it "should return the FB_APP_ID environment variable and thus not be null" do
FacebookPage.app_id.should_not be_empty
end
end
end
Run Code Online (Sandbox Code Playgroud)
但是当我跑步的时候
bundle exec rake spec
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
/home/geoffroy/.rvm/rubies/ruby-1.9.3-p194/bin/ruby -S rspec ./spec/models/facebook_page_spec.rb
/home/geoffroy/dev/mybandpage/spec/models/facebook_page_spec.rb:1:in `<top (required)>': uninitialized constant …Run Code Online (Sandbox Code Playgroud) 我写了*simple_form*输入扩展名,位于app/inputs/something_input.rb中
我正在尝试为此编写RSpec.当我把这个规范放在spec/helpers/application_helper_spec.rb中时,一切正常,没有任何问题.
# spec/helpers/application_helper_spec.rb
require 'spec_helper'
describe ApplicationHelper do
it do
helper.simple_form_for @foo,:method=>'get', :url=>helper.users_path do |f|
f.input :created_at, :as =>:custom_datepicker
end.should =~ /something/
end
end
Run Code Online (Sandbox Code Playgroud)
现在我正在尝试将该规范移动到spec/inputs/something_input_spec.rb,因此它将是类似的名称路径.
# spec/imputs/something_input_spec.rb
require 'spec_helper'
describe SomethingInput do
it do
helper.simple_form_for @foo,:method=>'get', :url=>helper.users_path do |f|
f.input :created_at, :as =>:custom_datepicker
end.should =~ /something/
end
end
#
#ERROR: undefined local variable or method `helper' for #<RSpec::Core::ExampleGroup
Run Code Online (Sandbox Code Playgroud)
我想告诉RSpec威胁这个文件作为类型帮助规范,所以我将使用所有RSpec :: Rails :: HelperExampleGroup功能的辅助方法
... 我怎样才能做到这一点 ??
我试图用RSpec …
我有一个rspec测试失败,但在开发中,当我自己执行操作时,页面呈现正确.
这是片段:
describe "sign up process" do
let (:admin1) { FactoryGirl.create(:admin) }
describe "with valid information"
before do
visit new_admin_registration_path
fill_in "Email", with: admin1.email
fill_in "Password", with: admin1.password
fill_in "Password confirmation", with: admin1.password_confirmation
click_button "Sign up"
end
it { should have_selector('div.alert') }
end
Run Code Online (Sandbox Code Playgroud)
根本没有找到那个选择器,但我确认它在使用开发环境查看源时存在.
我希望能够做的是检查正在渲染的页面以查看我缺少的内容.有没有办法做到这一点?
我有一个功能规格,如spec/features/awesome_feature_spec.rb需要spec/shared_examples/awesome_spec.rb.后者包含我正在使用的所有shared_examples awesome_feature_spec.rb.当一个示例失败并且我编辑一个文件来修复它并保存它时,guard会尝试再次运行该示例,但它会直接运行awesome_feature.rb而不是awesome_feature_spec.rb因为失败的共享示例所在awesome_feature.rb.这当然会导致错误,因为它需要运行awesome_feature_spec.rb,这是实际的功能规范.
这就是我的Guardfile的样子:
guard :rspec do
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
watch('spec/spec_helper.rb') { "spec" }
# Rails example
watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
watch(%r{^app/(.*)(\.erb|\.haml|\.slim)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
watch('config/routes.rb') { "spec/routing" }
watch('app/controllers/application_controller.rb') { "spec/controllers" }
# Capybara features specs
watch(%r{^app/views/(.+)/.*\.(erb|haml|slim)$}) { |m| "spec/features/#{m[1]}_spec.rb" }
watch(%r{^spec/shared_examples.*/(.+)\.rb$}) { |m| "spec/features/#{m[1]}_spec.rb" }
# Turnip features and steps …Run Code Online (Sandbox Code Playgroud) 我对rails很新,还在学习它.现在我得到了这样的测试失败消息:
1) LayoutLinks should have the right links on the layout
Failure/Error: click_link("Help")
Capybara::Ambiguous:
Ambiguous match, found 2 elements matching link "Help"
# ./spec/requests/layout_links_spec.rb:47:in `block (2 levels) in <top (required)>'
Run Code Online (Sandbox Code Playgroud)
因为我在我的主页上有两个相同的"帮助"链接,但似乎测试认为这是一个模糊的匹配,我希望它以相同的方式测试它们,因为链接指向同一页面.
实际上网站上有几种解决方案,但由于我很新,所以大多数解决方案都很复杂,所以有人能给我最简单的解决方案吗?提前致谢.
更新:
在我的观点和规范中没有什么复杂的.我还在学习一切.
这是我的规格
visit root_path
expect(page).to have_title("Home")
click_link("About")
expect(page).to have_title("About")
click_link("Contact")
expect(page).to have_title("Contact")
click_link("Help")
expect(page).to have_title("Help")
Run Code Online (Sandbox Code Playgroud)
这是我的标题部分在布局中
<header>
<nav class="round">
<ul>
<li><%= link_to "Home", root_path %></li>
<li><%= link_to "Help", help_path %></li>
<li><%= link_to "Sign in", "#" %></li>
</ul>
</nav>
</header>
Run Code Online (Sandbox Code Playgroud)
这是我的页脚部分布局
<footer>
<nav class="round">
<ul>
<li><%= link_to "About", …Run Code Online (Sandbox Code Playgroud) 我有以下文件夹结构:
app
??? assets
??? controllers
??? helpers
??? mailers
??? market_adapters
? ??? german.rb
?...
Run Code Online (Sandbox Code Playgroud)
该文件market_adapters/german.rb是:
module MarketAdapters #(I've also tried naming it singular)
class German
end
end
Run Code Online (Sandbox Code Playgroud)
运行测试时,我收到错误:
/gems/activesupport-5.0.0/lib/active_support/dependencies.rb:512:in
`load_missing_constant': Unable to autoload constant German,
expected .../app/market_adapters/german.rb to define it (LoadError)
Run Code Online (Sandbox Code Playgroud)
添加market_adapters文件夹autoload_paths似乎没有任何效果config.autoload_paths << "#{Rails.root}/app/market_adapters"
如果我移动market_adapters到lib文件夹,一切正常.但仍然想拥有它app,任何想法?
顺便说一下,我正在使用Rails 5.
rspec-rails ×10
rspec ×7
ruby ×2
actionmailer ×1
capybara ×1
controller ×1
guard ×1
rspec2 ×1
simple-form ×1
testing ×1