How to test that private method is called using rspec - ruby-on-rails-4

I'm trying to make sure that a given method is being called when called by a callback.
Basically, I have a RiskMatrix model, that calls 2 private methods on callback after_save
So I'm trying to test that those methods are properly called.
thanks a lot for your help
class RiskMatrix < ActiveRecord::Base
after_save :alert_admin, :save_suspicious_ip, if: proc {score.length >= ALERT_THRESHOLD}
private
def alert_admin
[...]
end
def save_suspicious_ip
[...]
end
end
risk_matrix_spec.rb
describe 'after_save' do
context 'score.length > ALERT_THRESHOLD' do
it 'should run alert_admin' do
matrix = build(:risk_matrix, score: 'ABCD')
expect(matrix).to receive(:alert_admin)
end
it 'should run save_suspicious_ip' do
matrix = create(:risk_matrix, score: 'ABCD')
expect(matrix).to receive(:save_suspicious_ip)
end
end
end
both tests fail
(#<RiskMatrix id: 3201, score: "ABCD", user_id: 3115, created_at: "2019-02-05 16:27:01", updated_at: "2019-02-05 16:27:01">).alert_admin(*(any args))
expected: 1 time with any arguments
received: 0 times with any arguments
(#<RiskMatrix id: nil, score: "ABCD", user_id: nil, created_at: nil, updated_at: nil>).save_suspicious_ip(*(any args))
expected: 1 time with any arguments
received: 0 times with any arguments

You can use shoulda-callback-matchers to test your callbacks
it { is_expected.to callback(:alert_admin).after(:save) }
The other way to test is verify that after save a matrix a suspicious_ip must exist.
let(:matrix) { create(:risk_matrix, score: 'ABCD') }
context "when score.length > ALERT_THRESHOLD" do
it "after create a matrix" do
expect(matrix.suspicious_ip).to_not be_nil
end
end
context "when score.length < ALERT_THRESHOLD" do
it "after create a matrix" do
expect(matrix.suspicious_ip).to be_nil
end
end

Maybe I am missing something but I am not seeing the save calls, have you tried:
it 'should run alert_admin' do
matrix = build(:risk_matrix, score: 'ABCD')
allow(matrix).to receive(:alert_admin)
matrix.save!
expect(matrix).to have_received(:alert_admin).once
end
Before you save you let RSpec know you gonna stub that method, after the save you verify the method was called.

Related

Lua : attempt to index a nil value; avoiding errors in conditionals

Let's say I have a giant table, something like:
test.test[1].testing.test.test_test
The table isn't guaranteed to exist. Neither are the tables containing it. I would like to just be able to do:
if test.test[1].testing.test.test_test then
print("it exits!")
end
But of course, this would give me an "Attempt to index ? (a nil value)" error if any of the indices aren't yet defined. So many times, I'll end up doing something like this:
if test then
if test.test then
if test.test[1] then
if test.test[1].testing then -- and so on
Is there a better, less-tedious way to accomplish this?
You can write a function that takes a list of keys to look up and does whatever action you want if it finds the entry. Here's an example:
function forindices(f, table, indices)
local entry = table
for _,idx in ipairs(indices) do
if type(entry) == 'table' and entry[idx] then
entry = entry[idx]
else
entry = nil
break
end
end
if entry then
f()
end
end
test = {test = {{testing = {test = {test_test = 5}}}}}
-- prints "it exists"
forindices(function () print("it exists") end,
test,
{"test", 1, "testing", "test", "test_test"})
-- doesn't print
forindices(function () print("it exists") end,
test,
{"test", 1, "nope", "test", "test_test"})
As an aside, the functional programming concept that solves this kind of problem is the Maybe monad. You could probably solve this with a Lua implementation of monads, though it wouldn't be very nice since there's no syntactic sugar for it.
You can avoid raising errors by setting an __index metamethod for nil:
debug.setmetatable(nil, { __index=function () end })
print(test.test[1].testing.test.test_test)
test = {test = {{testing = {test = {test_test = 5}}}}}
print(test.test[1].testing.test.test_test)
You also use an empty table:
debug.setmetatable(nil, { __index={} })

slick 3 two leftJoin query result to classes mapping

Current question is relative with next one, but now I need to read the data from database instead of insert.
I have next three case classes:
case class A (id: Long, bList: List[B])
case class B (id: Long, aId: cList: List[C])
case class C (id: Long, bId: Long)
And query with two leftJoin functions and incomingAId for filtering aTable results:
val query = (for {
((aResult,bResult),cResult) <- aTable.filter(_.id === incomigAId)
.joinLeft(bTable).on(_.id === _.aId)
.joinLeft(cTable).on(_._2.map(_.id) === _.bId)
} yield ((aResult,bResult),cResult)).result.transactionally
Next query works and the result looks valid, but isn't easy to handle it to the case classes. Also, executionResult has Seq[Nothing] type and process of mapping requires something like that:
database.run(query).map{ executionResult =>
executionResult.map { vectorElement: [Tuple2[Tuple2[A, Option[B]], Option[C]]]
...
}
}
Is there any proper way to prevent Seq[Nothing] (changes in query)?
Or if the query result type is fine, could you please share solution how to map it to the case classes above?
Right now I'm using next solution, but I suppose that some part of code can be optimized (e.g. groupBy replacing with something else).
execute(query).mapTo[Vector[((A, Option[B]), Option[C])]]
.flatMap { insideFuture =>
insideFuture.groupBy(_._1._1).mapValues { values =>
//scala groupBy losts ordering
val orderedB = values.groupBy(_._1._2).toSeq.sortBy(_._1.map(_.id)).toMap
orderedB.mapValues(_.map(_._2))
}.headOption match {
case Some(data) => Future.successful {
data._1.copy(bList = data._2.map {
case (Some(bElement), optionCElements) =>
bElement.copy(cList = optionCElements.toList.flatten)
case _ =>
throw new Exception("Invalid query result. Unable to find B elements")
}.toList)
}
case None => Future.failed(new Exception("Unable to find A with next id " + incomigAId))
}
}
}

Test exceptions using espec in Elixir app

I'm just started to learn Elixir.
I'm trying to test exception with espec (https://github.com/antonmi/espec) and I stucked.
Here is my function
defp call_xml(request_body) do
resp = HTTPotion.post("http://foo.bar", [body: request_body, headers: ["Content-Type": "text/xml"]])
if resp.status_code in [200, 201] do
{:ok, elem(XMLRPC.decode(resp.body), 1).param}
else
raise AbpError, message: "Message body"
end
end
def create_some_stuff(a, b, c) do
req = %XMLRPC.MethodCall{method_name: "Foo.Bar",
params:[a, b, c]} |> XMLPRC.encode!
call_xml(req)
end
# tests
use ESpec
use HyperMock
import :meck
context "when exception rised" do
it "returns err message" do
# stubbed with :meck
expect(MyModule, : create_some_stuff, fn("foo", "bar", "baz") -> raise CustomError end)
expect(MyModule. create_some_stuff("foo", "bar", "baz")).to eq("???")
end # it
end
In that case I'm getting error raised in my expectation
** (AbpError) Error occured!
spec/lib/ex_abpclient_spec.exs:135: anonymous fn/7 in ExAbpclientSpec.example_returns_created_payback_eqcpjlrszudikwyovtmxbgfnha/1
(ex_abpclient) ExAbpclient.create_payment_payback("tr-TR", 10966, 10, "R", 495, 10, "DESC")
spec/lib/ex_abpclient_spec.exs:136: ExAbpclientSpec.example_returns_created_payback_eqcpjlrszudikwyovtmxbgfnha/1
(espec) lib/espec/example_runner.ex:33: ESpec.ExampleRunner.run_example/2
(elixir) lib/enum.ex:1088: Enum."-map/2-lists^map/1-0-"/2
(elixir) lib/enum.ex:1088: Enum."-map/2-lists^map/1-0-"/2
(espec) lib/espec/runner.ex:70: ESpec.Runner.run_examples/1
(espec) lib/espec/runner.ex:43: ESpec.Runner.do_run/2
How can I get stubbed exception?
Thanks in advance.
UPDATE
I tried to use HyperMock (https://github.com/stevegraham/hypermock) to stub the request, but with no luck too
context "when payback created" do
it "returns created payback" do
HyperMock.intercept do
request = %Request{body: "<?xml version=\"1.0\" encoding=\"UTF-8\"?><methodCall>.....",
headers: ["Content-Type": "text/xml"],
method: :post,
uri: "http://foo.bar/webApiXmlRpcServlet"}
response = %Response{body: "fooooo", status: 500}
stub_request request, response
expect MyModule.create_some_stuff("a", "b", "c") |> to(raise_exception AbpError, "fooooo")
end
end # it
end # exception
Here is the result
/Users/retgoat/workspace/offside/ex_abpclient/spec/lib/ex_abpclient_spec.exs:135
** (AbpError) Error: "fooooo"
(ex_abpclient) lib/ex_abpclient.ex:55: ExAbpclient.call_xml/1
spec/lib/ex_abpclient_spec.exs:143: ExAbpclientSpec.example_returns_created_payback_nqfwohpurlvtzskdjxigeybamc/1
(espec) lib/espec/example_runner.ex:33: ESpec.ExampleRunner.run_example/2
(elixir) lib/enum.ex:1088: Enum."-map/2-lists^map/1-0-"/2
(elixir) lib/enum.ex:1088: Enum."-map/2-lists^map/1-0-"/2
(espec) lib/espec/runner.ex:70: ESpec.Runner.run_examples/1
(espec) lib/espec/runner.ex:43: ESpec.Runner.do_run/2
(espec) lib/espec/runner.ex:28: ESpec.Runner.handle_call/3
10 examples, 1 failures
Finished in 1.28 seconds (0.14s on load, 1.14s on specs)
Exception is rised, but I can't test it.
Roman!
You must pass a function to expect, not a result of a function call.
So, just wrap MyModule.create_some_stuff("a", "b", "c") by fn -> end like you do in the ExUnit expample:
elixir
it "raises exception" do
expect(fn -> MyModule.create_some_stuff("a", "b", "c") end)
|> to(raise_exception AbpError, "fooooo")
end

if condition for amount in USD $ in rails

My requirement is to show button only if my #order.display_total < $200
so when I have included given code:
- if method.method_type == 'cashondelivery' && #order.display_total < $200
.form-buttons{"data-hook" => "buttons"}
= form.hidden_field :cod_pay, :value => true
= submit_tag "Order Now", :class => 'order-now btn btn-danger'
it gives me error :
NoMethodError - undefined method `<' for #<Spree::Money:0x007ff3d9366490>:
where as #order fetch this:
#<Spree::Order id: 5964, number: "R981938713", item_total: #<BigDecimal:7ff3d2514f78,'0.3843E3',18(18)>, total: #<BigDecimal:7ff3cea3bd20,'0.3843E3',18(18)>, state: "address", adjustment_total: #<BigDecimal:7ff3d25149b0,'0.0',9(18)>, user_id: 1, completed_at: nil, bill_address_id: 24481, ship_address_id: 24482, payment_total: #<BigDecimal:7ff3d25142f8,'0.0',9(18)>, shipping_method_id: nil, shipment_state: nil, payment_state: nil, email: "admin#skinnymint.com", special_instructions: nil, created_at: "2015-12-27 03:45:02", updated_at: "2015-12-28 12:30:34", currency: "USD", last_ip_address: "127.0.0.1", created_by_id: 1, shipment_total: #<BigDecimal:7ff3d251eb68,'0.0',9(18)>, additional_tax_total: #<BigDecimal:7ff3d251ea00,'0.0',9(18)>, promo_total: #<BigDecimal:7ff3d251e7d0,'0.0',9(18)>, channel: "spree", included_tax_total: #<BigDecimal:7ff3d251e5c8,'0.0',9(18)>, item_count: 7, approver_id: nil, approved_at: nil, confirmation_delivered: false, considered_risky: false, guest_token: "aGoCAkyLXJs1oOUp9dS96w", locale: nil, state_lock_version: 0, cod_pay: false>
and #order.display_total = $398.40
Please guide me how to put if condition for this as I am new in spree rails. Thanks in advance.
#order.display_total returns a "money" object that does not have a comparison. However I notice that your object also has an "item_total" field, that appears to have the numeric value. So #order.item_total < 200 will work. However this will not take into account monetary conversion etc.
Your conditional is comparing different types. The less than sign can only be used to compare numbers. You will need to convert #order.display_total to a numeric and remove the dollar sign from the right side of the comparison.
Even better would be to move that logic to a helper method, thereby keeping the view free from logic.
I suggest to compare the amount without '$' I'm not sure how you store the amount so in case you can compare numbers directly it's the easiest solution to use.
Otherwise create a simple method that extract number and compare them:
def is_allowed?(amount,total)
nbr1 = amount[1..-1]
nbr2 = total[1..-1]
amount < total
end
Dont use $. If order.display_total is a string: cast to int/double/float and then try to test it with < 200

Faking an enumerator in FakeItEasy

How can I create a fake with FakeItEasy that allows different return values on successive calls. This is one example of what I would like to be able to do:
var enumerator = A.Fake<IDictionaryEnumerator>();
A.CallTo(() => enumerator.MoveNext()).Returns(true); //Expected value for first call
A.CallTo(() => enumerator.Key).Returns("key1");
A.CallTo(() => enumerator.Value).Returns("value1");
A.CallTo(() => enumerator.MoveNext()).Returns(false); //Expected value for second call
Assert.IsTrue(enumerator.MoveNext()); //Fails
Assert.IsFalse(enumerator.MoveNext());
The assertion will fail since it the last set up of MoveNext will overwrite the first one.
Is it possible to do what I want in FakeItEasy?
.
[Edit]
Clarified the original question's example and provided a working example below.
Based on Patrik's answer this code shows how you can set up the fake. The trick is to reverse all setups and use Once().
var enumerator = A.Fake<IDictionaryEnumerator>();
A.CallTo(() => enumerator.MoveNext()).Returns(false).Once();
A.CallTo(() => enumerator.MoveNext()).Returns(true).NumberOfTimes(2);
A.CallTo(() => enumerator.Key).Returns("key2").Once();
A.CallTo(() => enumerator.Value).Returns("value2").Once();
A.CallTo(() => enumerator.Key).Returns("key1").Once();
A.CallTo(() => enumerator.Value).Returns("value1").Once();
while(enumerator.MoveNext())
{
Debug.WriteLine(enumerator.Key + ": "+ enumerator.Value);
}
This will print:
key1: value1
key2: value2
I'm not entirely sure that I understand what you mean, the code you supplied will always fail. However if you mean that you want it to return true the second time it's called it can be done. There are a couple of different ways that I can think of, two of them are:
A.CallTo(() => enumerator.MoveNext()).ReturnsNextFromSequence(false, true);
The other way is:
A.CallTo(() => enumerator.MoveNext()).Returns(true);
A.CallTo(() => enumerator.MoveNext()).Returns(false).Once();
Edit:
On second though I guess I understand your question better, what you want to happen is that MoveNext should return true the first time and false the second time? If that's the case just change the orders of the values in the examples above.
FakeItEasy does not use a record/replay model and you are correct in that the latest configured rule has precedence over any earlier specified rules. That's why you have to specify repeat - ".Once()" - on the latest configuration for it only to be valid once.
There are many reasons why the latest has precedence, one of the most important ones is that it lets you set up a default return value in the setup of your fixture and override it to return specific values in some of your tests, this is impossible when using a record/replay model.
The OP's example based on Patrik's answer is fine ... but tedious if the sequence grows large. To supplement that answer consider that even though fake/mock examples most often show a bunch of straight line code to Arrange themselves you actually have the full power of a programming language at your command. Conditionals, loops, and even procedures.
So consider the following:
public static void AFakeDictionaryEnumeratorReturns(
IDictionaryEnumerator enumerator, params object[] pairs)
{
if (0 != pairs.Length % 2)
throw new ArgumentException("pairs must have even number of elements", "pairs");
int n = pairs.Length / 2;
A.CallTo(() => enumerator.MoveNext()).Returns(false).Once();
A.CallTo(() => enumerator.MoveNext()).Returns(true).NumberOfTimes(n);
for (int i = pairs.Length; i > 0; i -= 2)
{
A.CallTo(() => enumerator.Key).Returns(pairs[i - 2]).Once();
A.CallTo(() => enumerator.Value).Returns(pairs[i - 1]).Once();
}
}
and now the test becomes:
var enumerator = A.Fake<IDictionaryEnumerator>();
AFakeDictionaryEnumeratorReturns(enumerator,
"key1", "value1", "key2", "value2", "key3", "value3");
var keys = new List<object>();
var values = new List<object>();
while (enumerator.MoveNext())
{
keys.Add(enumerator.Key);
values.Add(enumerator.Value);
}
Assert.Equal(new List<object> { "key1", "key2", "key3" }, keys);
Assert.Equal(new List<object> { "value1", "value2", "value3" }, values);
(An IDictionaryEnumerator deals in pairs, so this example isn't as clearly beneficial as it could be. For a standard IEnumerator<T> a single static generic method would serve for a whole bunch of different enumerators.)